/// <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 = ThroughProperty.PropertyType.New <IList>(); ThroughProperty.SetValue(entity, throughRelationshipCollection); foreach (IIdentifiable pointer in (IList)newValue) { var throughInstance = ThroughType.New(); 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) { if (resource == null) { throw new ArgumentNullException(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); } }
/// <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); } }