/// <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);
            }
        }
        /// <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>
        /// 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);
                }
            }
        }
Beispiel #4
0
        /// <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);
            }
        }