/// <summary> /// Removes the <paramref name="item"/> from the binding graph /// </summary> /// <param name="item">Item to remove</param> /// <param name="parent">Parent of the <paramref name="item"/></param> /// <param name="parentProperty">Parent property that refers to <paramref name="item"/></param> public void Remove(object item, object parent, string parentProperty) { Vertex vertexToRemove = this.graph.LookupVertex(item); if (vertexToRemove == null) { return; } Debug.Assert(!vertexToRemove.IsRootCollection, "Root collections are never removed"); // Parent will always be non-null for deletes from collections, this will include // both root and child collections. For root collections, parentProperty will be null. Debug.Assert(parent != null, "Parent has to be present."); // When parentProperty is null, parent is itself a root collection if (parentProperty != null) { BindingEntityInfo.BindingPropertyInfo bpi = BindingEntityInfo.GetObservableProperties(parent.GetType()) .Single(p => p.PropertyInfo.PropertyName == parentProperty); Debug.Assert(bpi.PropertyKind == BindingPropertyKind.BindingPropertyKindCollection, "parentProperty must refer to an DataServiceCollection"); parent = bpi.PropertyInfo.GetValue(parent); } object source = null; string sourceProperty = null; string sourceEntitySet = null; string targetEntitySet = null; this.GetEntityCollectionInfo( parent, out source, out sourceProperty, out sourceEntitySet, out targetEntitySet); targetEntitySet = BindingEntityInfo.GetEntitySet(item, targetEntitySet); this.observer.HandleDeleteEntity( source, sourceProperty, sourceEntitySet, parent as ICollection, item, targetEntitySet); this.graph.RemoveEdge(parent, item, null); }
public bool AddEntity(object source, string sourceProperty, object target, string targetEntitySet, object edgeSource) { Vertex vertex = this.graph.LookupVertex(edgeSource); Vertex vertex2 = null; bool flag = false; if (target != null) { vertex2 = this.graph.LookupVertex(target); if (vertex2 == null) { vertex2 = this.graph.AddVertex(target); vertex2.EntitySet = BindingEntityInfo.GetEntitySet(target, targetEntitySet, this.observer.Context.MaxProtocolVersion); if (!this.AttachEntityOrComplexObjectNotification(target)) { throw new InvalidOperationException(System.Data.Services.Client.Strings.DataBinding_NotifyPropertyChangedNotImpl(target.GetType())); } flag = true; } if (this.graph.ExistsEdge(edgeSource, target, vertex.IsDataServiceCollection ? null : sourceProperty)) { throw new InvalidOperationException(System.Data.Services.Client.Strings.DataBinding_EntityAlreadyInCollection(target.GetType())); } this.graph.AddEdge(edgeSource, target, vertex.IsDataServiceCollection ? null : sourceProperty); } if (!vertex.IsDataServiceCollection) { this.observer.HandleUpdateEntityReference(source, sourceProperty, vertex.EntitySet, target, (vertex2 == null) ? null : vertex2.EntitySet); } else { this.observer.HandleAddEntity(source, sourceProperty, (vertex.Parent != null) ? vertex.Parent.EntitySet : null, edgeSource as ICollection, target, vertex2.EntitySet); } if (flag) { this.AddFromProperties(target); } return(flag); }
public void RemoveDataServiceCollectionItem(object item, object parent, string parentProperty) { Func <BindingEntityInfo.BindingPropertyInfo, bool> predicate = null; if (this.graph.LookupVertex(item) != null) { if (parentProperty != null) { if (predicate == null) { predicate = p => p.PropertyInfo.PropertyName == parentProperty; } parent = BindingEntityInfo.GetObservableProperties(parent.GetType(), this.observer.Context.MaxProtocolVersion).Single <BindingEntityInfo.BindingPropertyInfo>(predicate).PropertyInfo.GetValue(parent); } object source = null; string sourceProperty = null; string sourceEntitySet = null; string targetEntitySet = null; this.GetDataServiceCollectionInfo(parent, out source, out sourceProperty, out sourceEntitySet, out targetEntitySet); targetEntitySet = BindingEntityInfo.GetEntitySet(item, targetEntitySet, this.observer.Context.MaxProtocolVersion); this.observer.HandleDeleteEntity(source, sourceProperty, sourceEntitySet, parent as ICollection, item, targetEntitySet); this.graph.RemoveEdge(parent, item, null); } }
public bool AddEntity( object source, string sourceProperty, object target, string targetEntitySet, object edgeSource) { Vertex sourceVertex = this.graph.LookupVertex(edgeSource); Debug.Assert(sourceVertex != null, "Must have a valid edge source"); Vertex entityVertex = null; bool addedNewEntity = false; if (target != null) { entityVertex = this.graph.LookupVertex(target); if (entityVertex == null) { entityVertex = this.graph.AddVertex(target); entityVertex.EntitySet = BindingEntityInfo.GetEntitySet(target, targetEntitySet); if (!this.AttachEntityOrComplexObjectNotification(target)) { throw new InvalidOperationException(Strings.DataBinding_NotifyPropertyChangedNotImpl(target.GetType())); } addedNewEntity = true; } if (this.graph.ExistsEdge(edgeSource, target, sourceVertex.IsCollection ? null : sourceProperty)) { throw new InvalidOperationException(Strings.DataBinding_EntityAlreadyInCollection(target.GetType())); } this.graph.AddEdge(edgeSource, target, sourceVertex.IsCollection ? null : sourceProperty); } if (!sourceVertex.IsCollection) { this.observer.HandleUpdateEntityReference( source, sourceProperty, sourceVertex.EntitySet, target, entityVertex == null ? null : entityVertex.EntitySet); } else { Debug.Assert(target != null, "Target must be non-null when adding to collections"); this.observer.HandleAddEntity( source, sourceProperty, sourceVertex.Parent != null ? sourceVertex.Parent.EntitySet : null, edgeSource as ICollection, target, entityVertex.EntitySet); } if (addedNewEntity) { this.AddFromProperties(target); } return(addedNewEntity); }