/// <summary> /// Traverses through the provided resource and sets the value of the relationship on the other side of the through type. /// In the example described above, this would be the value of "Articles.ArticleTags.Tag". /// </summary> public override void SetValue(object resource, object newValue) { ArgumentGuard.NotNull(resource, nameof(resource)); base.SetValue(resource, newValue); if (newValue == null) { ThroughProperty.SetValue(resource, null); } else { List <object> throughResources = new List <object>(); foreach (IIdentifiable rightResource in (IEnumerable)newValue) { var throughEntity = TypeHelper.CreateInstance(ThroughType); LeftProperty.SetValue(throughEntity, resource); RightProperty.SetValue(throughEntity, rightResource); throughResources.Add(throughEntity); } var typedCollection = TypeHelper.CopyToTypedCollection(throughResources, ThroughProperty.PropertyType); ThroughProperty.SetValue(resource, typedCollection); } }
static RandomCanvas() { LeftProperty.OverrideMetadata(typeof(RandomCanvas), new FrameworkPropertyMetadata(double.NaN, OnPositioningChanged)); TopProperty.OverrideMetadata(typeof(RandomCanvas), new FrameworkPropertyMetadata(double.NaN, OnPositioningChanged)); RightProperty.OverrideMetadata(typeof(RandomCanvas), new FrameworkPropertyMetadata(double.NaN, OnPositioningChanged)); BottomProperty.OverrideMetadata(typeof(RandomCanvas), new FrameworkPropertyMetadata(double.NaN, OnPositioningChanged)); }
/// <summary> /// Sets the value of the property identified by this attribute /// </summary> /// <param name="entity">The target object</param> /// <param name="newValue">The new property value</param> public override void SetValue(object entity, object newValue) { var propertyInfo = entity .GetType() .GetProperty(InternalRelationshipName); propertyInfo.SetValue(entity, newValue); if (newValue == null) { ThroughProperty.SetValue(entity, null); } else { var throughRelationshipCollection = (IList)Activator.CreateInstance(ThroughProperty.PropertyType); ThroughProperty.SetValue(entity, throughRelationshipCollection); foreach (IIdentifiable pointer in (IList)newValue) { var throughInstance = Activator.CreateInstance(ThroughType); LeftProperty.SetValue(throughInstance, entity); RightProperty.SetValue(throughInstance, pointer); throughRelationshipCollection.Add(throughInstance); } } }
/// <summary> /// Traverses through the provided resource and sets the value of the relationship on the other side of the through type. /// In the example described above, this would be the value of "Articles.ArticleTags.Tag". /// </summary> public override void SetValue(object resource, object newValue, IResourceFactory resourceFactory) { if (resource == null) { throw new ArgumentNullException(nameof(resource)); } if (resourceFactory == null) { throw new ArgumentNullException(nameof(resourceFactory)); } base.SetValue(resource, newValue, resourceFactory); if (newValue == null) { ThroughProperty.SetValue(resource, null); } else { List <object> throughResources = new List <object>(); foreach (IIdentifiable identifiable in (IEnumerable)newValue) { object throughResource = resourceFactory.CreateInstance(ThroughType); LeftProperty.SetValue(throughResource, resource); RightProperty.SetValue(throughResource, identifiable); throughResources.Add(throughResource); } var typedCollection = TypeHelper.CopyToTypedCollection(throughResources, ThroughProperty.PropertyType); ThroughProperty.SetValue(resource, typedCollection); } }
/// <summary> /// Traverses through the provided resource and returns the /// value of the relationship on the other side of a through type /// (e.g. Articles.ArticleTags.Tag). /// </summary> public override object GetValue(object resource) { IEnumerable throughResources = (IEnumerable)ThroughProperty.GetValue(resource) ?? Array.Empty <object>(); IEnumerable <object> rightResources = throughResources .Cast <object>() .Select(rightResource => RightProperty.GetValue(rightResource)); return(rightResources.CopyToTypedCollection(Property.PropertyType)); }
/// <summary> /// Traverses through the provided entity and returns the /// value of the relationship on the other side of a join entity /// (e.g. Articles.ArticleTags.Tag). /// </summary> public override object GetValue(object entity) { IEnumerable joinEntities = (IEnumerable)ThroughProperty.GetValue(entity) ?? Array.Empty <object>(); IEnumerable <object> rightEntities = joinEntities .Cast <object>() .Select(rightEntity => RightProperty.GetValue(rightEntity)); return(rightEntities.CopyToTypedCollection(PropertyInfo.PropertyType)); }
/// <summary> /// Traverses through the provided resource and returns the value of the relationship on the other side of the through type. /// In the example described above, this would be the value of "Articles.ArticleTags.Tag". /// </summary> public override object GetValue(object resource) { if (resource == null) { throw new ArgumentNullException(nameof(resource)); } IEnumerable throughResources = (IEnumerable)ThroughProperty.GetValue(resource) ?? Array.Empty <object>(); IEnumerable <object> rightResources = throughResources .Cast <object>() .Select(rightResource => RightProperty.GetValue(rightResource)); return(TypeHelper.CopyToTypedCollection(rightResources, Property.PropertyType)); }
/// <summary> /// Traverses through the provided resource and returns the value of the relationship on the other side of the through type. /// In the example described above, this would be the value of "Articles.ArticleTags.Tag". /// </summary> public override object GetValue(object resource) { ArgumentGuard.NotNull(resource, nameof(resource)); var throughEntity = ThroughProperty.GetValue(resource); if (throughEntity == null) { return(null); } IEnumerable <object> rightResources = ((IEnumerable)throughEntity) .Cast <object>() .Select(rightResource => RightProperty.GetValue(rightResource)); return(TypeHelper.CopyToTypedCollection(rightResources, Property.PropertyType)); }
/// <inheritdoc /> public override void SetValue(object entity, object newValue, IResourceFactory resourceFactory) { base.SetValue(entity, newValue, resourceFactory); if (newValue == null) { ThroughProperty.SetValue(entity, null); } else { List <object> joinEntities = new List <object>(); foreach (IIdentifiable resource in (IEnumerable)newValue) { object joinEntity = resourceFactory.CreateInstance(ThroughType); LeftProperty.SetValue(joinEntity, entity); RightProperty.SetValue(joinEntity, resource); joinEntities.Add(joinEntity); } var typedCollection = joinEntities.CopyToTypedCollection(ThroughProperty.PropertyType); ThroughProperty.SetValue(entity, typedCollection); } }
/// <summary> /// Traverses the through the provided entity and returns the /// value of the relationship on the other side of a join entity /// (e.g. Articles.ArticleTags.Tag). /// </summary> public override object GetValue(object entity) { var throughNavigationProperty = entity.GetType() .GetProperties() .SingleOrDefault(p => string.Equals(p.Name, InternalThroughName, StringComparison.OrdinalIgnoreCase)); var throughEntities = throughNavigationProperty.GetValue(entity); if (throughEntities == null) { // return an empty list for the right-type of the property. return(TypeHelper.CreateListFor(RightType)); } // the right entities are included on the navigation/through entities. Extract and return them. var rightEntities = new List <IIdentifiable>(); foreach (var rightEntity in (IList)throughEntities) { rightEntities.Add((IIdentifiable)RightProperty.GetValue(rightEntity)); } return(rightEntities.Cast(RightType)); }