/// <summary>
        /// Records a removal to collection valued properties on SelfTracking Entities.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="value">The object value.</param>
        internal void RecordRemovalFromCollectionProperties(string propertyName, object value)
        {
            if (objectTrackingEnabledField)
            {
                // Delete the entity back after adding it, we should do nothing here then
                if (ObjectsAddedToCollectionProperties.ContainsKey(propertyName) &&
                    ObjectsAddedToCollectionProperties[propertyName].Contains(value))
                {
                    ObjectsAddedToCollectionProperties[propertyName].Remove(value);
                    if (ObjectsAddedToCollectionProperties[propertyName].Count == 0)
                    {
                        ObjectsAddedToCollectionProperties.Remove(propertyName);
                    }
                    return;
                }

                if (!ObjectsRemovedFromCollectionProperties.ContainsKey(propertyName))
                {
                    ObjectsRemovedFromCollectionProperties[propertyName] = new ObjectList();
                    ObjectsRemovedFromCollectionProperties[propertyName].Add(value);
                }
                else
                {
                    if (!ObjectsRemovedFromCollectionProperties[propertyName].Contains(value))
                    {
                        ObjectsRemovedFromCollectionProperties[propertyName].Add(value);
                    }
                }
            }
        }
        /// <summary>
        /// Trackables the collection changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="NotifyTrackableCollectionChangedEventArgs"/> instance containing the event data.</param>
        /// <param name="propertyName">Name of the property.</param>
        private void TrackableCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, string propertyName)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                foreach (var newItem in e.NewItems)
                {
                    //todo:implémenter la récursivité sur l'ajout des élements dans la collection
                    //var project = newItem as IObjectWithChangeTracker;
                    //if (project != null)
                    //{
                    //    if (ChangeTrackingEnabled)
                    //    {
                    //        project.ChangeTracker.Start();
                    //    }
                    //}
                    RecordAdditionToCollectionProperties(propertyName, newItem);
                }
                break;

            case NotifyCollectionChangedAction.Remove:
                foreach (var odlItem in e.OldItems)
                {
                    RecordRemovalFromCollectionProperties(propertyName, odlItem);
                }
                break;

            case NotifyCollectionChangedAction.Reset:
            {
                // Cas d'un Clear sur la collection.
                // Vide le cache des modifications pour la collection.
                if (ObjectsRemovedFromCollectionProperties.ContainsKey(propertyName))
                {
                    ObjectsRemovedFromCollectionProperties.Remove(propertyName);
                }

                if (ObjectsAddedToCollectionProperties.ContainsKey(propertyName))
                {
                    ObjectsAddedToCollectionProperties.Remove(propertyName);
                }

                // Tranfère les données de départ dans les élements supprimés.
                if (ObjectsOriginalFromCollectionProperties.Count > 0)
                {
                    foreach (var item in ObjectsOriginalFromCollectionProperties[propertyName])
                    {
                        RecordRemovalFromCollectionProperties(propertyName, item);
                    }
                }
            }
            break;

            case NotifyCollectionChangedAction.Replace:
            {
                // Comment gérer le cas d'un changement d'instance dans la liste ?
            }
            break;
            }
            UpdateChangeState();
        }
 // Resets the ObjectChangeTracker to the Unchanged state and
 // clears the original values as well as the record of changes
 // to collection properties
 public void ResetTracking()
 {
     if (objectTrackingEnabledField)
     {
         originalobjectStateField = ObjectState.Unchanged;
         ResetOriginalValue();
         ObjectsAddedToCollectionProperties.Clear();
         ObjectsRemovedFromCollectionProperties.Clear();
         ObjectsOriginalFromCollectionProperties.Clear();
         InitOriginalValue();
         UpdateChangeState();
     }
 }
Ejemplo n.º 4
0
        // Resets the ObjectChangeTracker to the Unchanged state and
        // rollback the original values as well as the record of changes
        // to collection properties
        public void CancelChanges()
        {
            OnObjectStateChanging(ObjectState.Unchanged);
            // rollback original values
            Type type = _parentObject.GetType();

            foreach (var originalValue in OriginalValues.ToList())
            {
                type.GetProperty(originalValue.Key).SetValue(
                    _parentObject, originalValue.Value, null);
            }
            // create copy of ObjectsAddedToCollectionProperties
            // and ObjectsRemovedFromCollectionProperties
            Dictionary <string, ObjectList> removeCollection =
                ObjectsAddedToCollectionProperties.ToDictionary(n => n.Key, n => n.Value);
            Dictionary <string, ObjectList> addCollection =
                ObjectsRemovedFromCollectionProperties.ToDictionary(n => n.Key, n => n.Value);

            // rollback ObjectsAddedToCollectionProperties
            if (removeCollection.Count > 0)
            {
                foreach (KeyValuePair <string, ObjectList> entry in removeCollection)
                {
                    PropertyInfo collectionProperty = type.GetProperty(entry.Key);
                    IList        collectionObject   = (IList)collectionProperty.GetValue(_parentObject, null);
                    foreach (object obj in entry.Value.ToList())
                    {
                        collectionObject.Remove(obj);
                    }
                }
            }
            // rollback ObjectsRemovedFromCollectionProperties
            if (addCollection.Count > 0)
            {
                foreach (KeyValuePair <string, ObjectList> entry in addCollection)
                {
                    PropertyInfo collectionProperty = type.GetProperty(entry.Key);
                    IList        collectionObject   = (IList)collectionProperty.GetValue(_parentObject, null);
                    foreach (object obj in entry.Value.ToList())
                    {
                        collectionObject.Add(obj);
                    }
                }
            }
            OriginalValues.Clear();
            ObjectsAddedToCollectionProperties.Clear();
            ObjectsRemovedFromCollectionProperties.Clear();
            _objectState = ObjectState.Unchanged;
            OnObjectStateChanged(ObjectState.Unchanged);
        }