Exemplo n.º 1
0
            /// <summary>
            /// Creates a new instance of <see cref="CollectionModification"/>.
            /// </summary>
            /// <param name="existingObjects">The current collection of objects.</param>
            /// <param name="updatedObjects">The collection containing objects that were imported
            /// and thus could contain updates for the existing objects.</param>
            /// <param name="equalityComparer">The comparer to test whether elements in
            /// <paramref name="existingObjects"/> have a matching element in
            /// <paramref name="updatedObjects"/>.</param>
            /// <exception cref="ArgumentNullException">Thrown if any parameter is <c>null</c>.</exception>
            /// <exception cref="ArgumentException">Thrown when <paramref name="existingObjects"/>
            /// or <paramref name="updatedObjects"/> contains a <c>null</c> element.</exception>
            public CollectionModification(IEnumerable <TTargetData> existingObjects,
                                          IEnumerable <TTargetData> updatedObjects,
                                          IEqualityComparer <TTargetData> equalityComparer)
            {
                if (existingObjects == null)
                {
                    throw new ArgumentNullException(nameof(existingObjects));
                }

                if (updatedObjects == null)
                {
                    throw new ArgumentNullException(nameof(updatedObjects));
                }

                if (equalityComparer == null)
                {
                    throw new ArgumentNullException(nameof(equalityComparer));
                }

                if (existingObjects.Contains(null))
                {
                    throw new ArgumentException(@"Cannot determine modifications from a collection which contain null.", nameof(existingObjects));
                }

                if (updatedObjects.Contains(null))
                {
                    throw new ArgumentException(@"Cannot determine modifications with a collection which contain null.", nameof(updatedObjects));
                }

                TTargetData[] existingArray = existingObjects.ToArray();

                var index = 0;

                foreach (TTargetData importedObject in updatedObjects)
                {
                    int existingObjectIndex = FindIndex(existingArray, importedObject, equalityComparer);
                    if (existingObjectIndex > -1)
                    {
                        ObjectsToBeUpdated.Add(index, existingArray[existingObjectIndex]);
                        existingArray[existingObjectIndex] = null;
                    }
                    else
                    {
                        ObjectsToBeAdded.Add(index, importedObject);
                    }

                    index++;
                }

                ObjectsToBeRemoved = existingArray.Where(e => e != null);
            }
Exemplo n.º 2
0
 /// <summary>
 /// Finds out whether there was a difference between the existing and the imported
 /// object collections.
 /// </summary>
 /// <returns><c>true</c> if there were any updates, <c>false</c> otherwise.</returns>
 public bool HasUpdates()
 {
     return(ObjectsToBeRemoved.Any() || ObjectsToBeAdded.Any() || ObjectsToBeUpdated.Any());
 }