Ejemplo n.º 1
0
        private void OnUpdateCollection()
        {
            // Get the source collection from the wrapped object.
            IEnumerable <T> sourceCollection = _getMethod();

            // Create a list of new items.
            List <CollectionItem <T> > items = new List <CollectionItem <T> >();

            // Dump all previous items into a recycle bin.
            using (RecycleBin <CollectionItem <T> > bin = new RecycleBin <CollectionItem <T> >())
            {
                foreach (T oldItem in _collection)
                {
                    bin.AddObject(new CollectionItem <T>(_collection, oldItem, true));
                }
                // Add new objects to the list.
                if (sourceCollection != null)
                {
                    foreach (T obj in sourceCollection)
                    {
                        items.Add(bin.Extract(new CollectionItem <T>(_collection, obj, false)));
                    }
                }
                // All deleted items are removed from the collection at this point.
            }
            // Ensure that all items are added to the list.
            int index = 0;

            foreach (CollectionItem <T> item in items)
            {
                item.EnsureInCollection(index);
                ++index;
            }
        }
        /// <summary>
        /// Moves all objects into a new recycle bin, from which they can be extracted.
        /// </summary>
        /// <param name="objects">A collection of objects to add to the bin.</param>
        /// <remarks>
        /// After the objects are added to the bin, the collection
        /// is cleared. Then it can be repopulated by extraction from
        /// the bin.
        /// </remarks>
        public static RecycleBin <T> Recycle <T>(this ICollection <T> collection)
        {
            RecycleBin <T> bin = new RecycleBin <T>(collection);

            if (collection != null)
            {
                collection.Clear();
            }

            return(bin);
        }