/// <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>
        /// 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);
                }
            }
        }
Esempio n. 3
0
        /// <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));
        }
Esempio n. 4
0
        /// <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));

            object throughEntity = ThroughProperty.GetValue(resource);

            if (throughEntity == null)
            {
                return(null);
            }

            IEnumerable <object> rightResources = ((IEnumerable)throughEntity).Cast <object>().Select(rightResource => RightProperty.GetValue(rightResource));

            return(CollectionConverter.CopyToTypedCollection(rightResources, Property.PropertyType));
        }
Esempio n. 5
0
        /// <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));
            }

            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));
        }
Esempio n. 6
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);
            }
        }