private void AddFromProperties(object entity) { foreach (BindingEntityInfo.BindingPropertyInfo info in BindingEntityInfo.GetObservableProperties(entity.GetType(), this.observer.Context.MaxProtocolVersion)) { object target = info.PropertyInfo.GetValue(entity); if (target != null) { switch (info.PropertyKind) { case BindingPropertyKind.BindingPropertyKindEntity: { this.AddEntity(entity, info.PropertyInfo.PropertyName, target, null, entity); continue; } case BindingPropertyKind.BindingPropertyKindDataServiceCollection: { this.AddDataServiceCollection(entity, info.PropertyInfo.PropertyName, target, null); continue; } case BindingPropertyKind.BindingPropertyKindPrimitiveOrComplexCollection: { this.AddPrimitiveOrComplexCollection(entity, info.PropertyInfo.PropertyName, target, info.PropertyInfo.PrimitiveOrComplexCollectionItemType); continue; } } this.AddComplexObject(entity, info.PropertyInfo.PropertyName, target); } } }
/// <summary> /// Tries to get the value of a property and corresponding BindingPropertyInfo or ClientPropertyAnnotation if the property exists /// </summary> /// <param name="source">Source object whose property needs to be read</param> /// <param name="sourceProperty">Name of the source object property</param> /// <param name="model">The client model.</param> /// <param name="bindingPropertyInfo">BindingPropertyInfo corresponding to <paramref name="sourceProperty"/></param> /// <param name="clientProperty">Instance of ClientProperty corresponding to <paramref name="sourceProperty"/></param> /// <param name="propertyValue">Value of the property</param> /// <returns>true if the property exists and the value was read; otherwise false.</returns> internal static bool TryGetPropertyValue(object source, string sourceProperty, ClientEdmModel model, out BindingPropertyInfo bindingPropertyInfo, out ClientPropertyAnnotation clientProperty, out object propertyValue) { Type sourceType = source.GetType(); bindingPropertyInfo = BindingEntityInfo.GetObservableProperties(sourceType, model) .SingleOrDefault(x => x.PropertyInfo.PropertyName == sourceProperty); bool propertyFound = bindingPropertyInfo != null; // bindingPropertyInfo is null for primitive properties. if (!propertyFound) { clientProperty = BindingEntityInfo.GetClientType(sourceType, model) .GetProperty(sourceProperty, true); propertyFound = clientProperty != null; if (!propertyFound) { propertyValue = null; } else { propertyValue = clientProperty.GetValue(source); } } else { clientProperty = null; propertyValue = bindingPropertyInfo.PropertyInfo.GetValue(source); } return(propertyFound); }
/// <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); }
internal static object GetPropertyValue(object source, string sourceProperty, out BindingPropertyInfo bindingPropertyInfo) { Type sourceType = source.GetType(); bindingPropertyInfo = BindingEntityInfo.GetObservableProperties(sourceType) .SingleOrDefault(x => x.PropertyInfo.PropertyName == sourceProperty); if (bindingPropertyInfo == null) { return(BindingEntityInfo.GetClientType(sourceType) .GetProperty(sourceProperty, false) .GetValue(source)); } else { return(bindingPropertyInfo.PropertyInfo.GetValue(source)); } }
/// <summary>Add items to the graph, from the <paramref name="entity"/> object's properties</summary> /// <param name="entity">Object whose properties are to be explored</param> private void AddFromProperties(object entity) { // Once the entity is attached to the graph, we need to traverse all it's properties // and add related entities and collections to this entity. foreach (BindingEntityInfo.BindingPropertyInfo bpi in BindingEntityInfo.GetObservableProperties(entity.GetType())) { object propertyValue = bpi.PropertyInfo.GetValue(entity); if (propertyValue != null) { switch (bpi.PropertyKind) { case BindingPropertyKind.BindingPropertyKindCollection: this.AddCollection( entity, bpi.PropertyInfo.PropertyName, propertyValue, null); break; case BindingPropertyKind.BindingPropertyKindEntity: this.AddEntity( entity, bpi.PropertyInfo.PropertyName, propertyValue, null, entity); break; default: Debug.Assert(bpi.PropertyKind == BindingPropertyKind.BindingPropertyKindComplex, "Must be complex type if PropertyKind is not entity or collection."); this.AddComplexProperty( entity, bpi.PropertyInfo.PropertyName, propertyValue); break; } } } }
private void AddFromProperties(object entity) { foreach (BindingEntityInfo.BindingPropertyInfo bpi in BindingEntityInfo.GetObservableProperties(entity.GetType())) { object propertyValue = bpi.PropertyInfo.GetValue(entity); if (propertyValue != null) { switch (bpi.PropertyKind) { case BindingPropertyKind.BindingPropertyKindCollection: this.AddCollection( entity, bpi.PropertyInfo.PropertyName, propertyValue, null); break; case BindingPropertyKind.BindingPropertyKindEntity: this.AddEntity( entity, bpi.PropertyInfo.PropertyName, propertyValue, null, entity); break; default: Debug.Assert(bpi.PropertyKind == BindingPropertyKind.BindingPropertyKindComplex, "Must be complex type if PropertyKind is not entity or collection."); this.AddComplexProperty( entity, bpi.PropertyInfo.PropertyName, propertyValue); break; } } } }
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); } }