예제 #1
0
        // Resets the ObjectChangeTracker to the Unchanged state and
        // sets the actual values to be the original values as well as the record of changes
        // to collection properties
        public void CancelChanges(IObjectWithChangeTracker entity)
        {
            if (this.State == ObjectState.Added)
            {
                return;
            }

            OnObjectStateChanging(ObjectState.Unchanged);
            ChangeTrackingEnabled = false;

            ModifiedValues.Clear();

            var originalValues = OriginalValues.ToArray();

            foreach (var kvp in originalValues)
            {
                string propertyName  = kvp.Key;
                object propertyValue = kvp.Value;

                entity.SetPropertyValue(propertyName, propertyValue);
            }

            var objectsAddedToCollectionProperties = ObjectsAddedToCollectionProperties.ToArray();

            foreach (var kvp in objectsAddedToCollectionProperties)
            {
                var propertyName = kvp.Key;
                var items        = kvp.Value;

                foreach (var item in items.ToArray())
                {
                    entity.RemoveItemFromCollection(propertyName, item);
                }
            }
            ObjectsAddedToCollectionProperties.Clear();

            var objectsRemovedFromCollectionProperties = ObjectsRemovedFromCollectionProperties.ToArray();

            foreach (var kvp in objectsRemovedFromCollectionProperties)
            {
                var propertyName = kvp.Key;
                var items        = kvp.Value;

                foreach (var item in items.ToArray())
                {
                    entity.AddItemToCollection(propertyName, item);
                }
            }
            ObjectsRemovedFromCollectionProperties.Clear();

            ChangeTrackingEnabled = true;
            _objectState          = ObjectState.Unchanged;
        }