Microsoft.ResourceManagement.ObjectModel.RmResourceChanges.GetDifference C# (CSharp) Method

GetDifference() private static method

Gets the differences from source and destination attributes.
private static GetDifference ( RmAttributeValue>.Dictionary sourceAttributes, RmAttributeValue>.Dictionary destinationAttributes ) : IList
sourceAttributes RmAttributeValue>.Dictionary The source attributes.
destinationAttributes RmAttributeValue>.Dictionary The destination attributes.
return IList
        private static IList<RmAttributeChange> GetDifference(
            Dictionary<RmAttributeName, RmAttributeValue> sourceAttributes,
            Dictionary<RmAttributeName, RmAttributeValue> destinationAttributes)
        {
            IList<RmAttributeChange> changedAttributes = new List<RmAttributeChange>();
            // iterate source attributes
            foreach (KeyValuePair<RmAttributeName, RmAttributeValue> sourceItem in sourceAttributes) {
                RmAttributeName sourceName = sourceItem.Key;
                RmAttributeValue sourceValue = sourceItem.Value;
                if (!destinationAttributes.ContainsKey(sourceName)) {
                    // the destination does not contain the attribute
                    if (sourceValue.IsMultiValue) {
                        // add all the values
                        foreach (IComparable value1 in sourceValue.Values) {
                            changedAttributes.Add(new RmAttributeChange(sourceName, value1, RmAttributeChangeOperation.Add));
                        }
                    } else {
                        changedAttributes.Add(new RmAttributeChange(sourceItem.Key, null, RmAttributeChangeOperation.Replace));
                    }
                } else {
                    // the destination contains the attribute
                    RmAttributeValue destinationValue = destinationAttributes[sourceItem.Key];
                    if (sourceValue.IsMultiValue) {
                        foreach (IComparable value1 in sourceValue.Values) {
                            if (destinationValue.Values.Contains(value1) == false) {
                                changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value1, RmAttributeChangeOperation.Add));
                            }
                        }
                        foreach (IComparable value2 in destinationValue.Values) {
                            if (sourceItem.Value.Values.Contains(value2) == false) {
                                changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value2, RmAttributeChangeOperation.Delete));
                            }
                        }
                    } else {
                        if (destinationValue.Value != sourceValue.Value) {
                            RmAttributeChangeOperation operation;
                            if (
                                (
                                // <[MA] change 24-11-2011> - cleared reference attributes require Delete operation
                                destinationValue.Value is RmReference
                                ||
                                // <[MA] change 05-03-2012> - cleared date attributes require Delete operation
                                destinationValue.Value is DateTime?
                                )
                                && sourceValue.Value == null
                                )
                            {
                                operation = RmAttributeChangeOperation.Delete;
                            }
                            else
                            {
                                operation = RmAttributeChangeOperation.Replace;
                            }
                            changedAttributes.Add(new RmAttributeChange(sourceItem.Key, sourceValue.Value, operation));
                            // </[MA]>
                        }
                    }
                }
            }
            // iterate destination attributes
            foreach (KeyValuePair<RmAttributeName, RmAttributeValue> destinationItem in destinationAttributes) {
                if (sourceAttributes.ContainsKey(destinationItem.Key) == false) {
                    foreach (IComparable value2 in destinationItem.Value.Values) {
                        changedAttributes.Add(new RmAttributeChange(destinationItem.Key, value2, RmAttributeChangeOperation.Delete));
                    }
                }
            }
            return changedAttributes;
        }

Same methods

RmResourceChanges::GetDifference ( RmResource source, RmResource destination ) : IList

Usage Example

示例#1
0
 /// <summary>
 /// Returns a list of changes made to this object since the transaction began or the last call to AcceptChanges.
 /// </summary>
 /// <returns></returns>
 public IList <RmAttributeChange> GetChanges()
 {
     EnsureNotDisposed();
     lock (rmObject.Attributes) {
         // there was no original, so we just return an empty list
         if (originalAttributes == null)
         {
             return(new List <RmAttributeChange>());
         }
         else
         {
             return(RmResourceChanges.GetDifference(rmObject.Attributes, this.originalAttributes));
         }
     }
 }