/// <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(); }