private object SetHasManyRelationship(object entity,
                                              PropertyInfo[] entityProperties,
                                              HasManyAttribute attr,
                                              ContextEntity contextEntity,
                                              Dictionary <string, RelationshipData> relationships,
                                              List <ResourceObject> included = null)
        {
            var relationshipName = attr.PublicRelationshipName;

            if (relationships.TryGetValue(relationshipName, out RelationshipData relationshipData))
            {
                if (relationshipData.IsHasMany == false || relationshipData.ManyData == null)
                {
                    return(entity);
                }

                var relatedResources = relationshipData.ManyData.Select(r =>
                {
                    var instance = GetIncludedRelationship(r, included, attr);
                    return(instance);
                });

                var convertedCollection = TypeHelper.ConvertCollection(relatedResources, attr.DependentType);

                attr.SetValue(entity, convertedCollection);
                /// todo: as a part of the process of decoupling JADNC (specifically
                /// through the decoupling IJsonApiContext), we now no longer need to
                /// store the updated relationship values in this property. For now
                /// just assigning null as value, will remove this property later as a whole.
                /// see #512
                _jsonApiContext.HasManyRelationshipPointers.Add(attr, null);
            }

            return(entity);
        }
        private object SetHasManyRelationship(object entity,
                                              PropertyInfo[] entityProperties,
                                              HasManyAttribute attr,
                                              ContextEntity contextEntity,
                                              Dictionary <string, RelationshipData> relationships,
                                              List <ResourceObject> included = null)
        {
            var relationshipName = attr.PublicRelationshipName;

            if (relationships.TryGetValue(relationshipName, out RelationshipData relationshipData))
            {
                if (relationshipData.IsHasMany == false || relationshipData.ManyData == null)
                {
                    return(entity);
                }

                var relatedResources = relationshipData.ManyData.Select(r =>
                {
                    var instance = GetIncludedRelationship(r, included, attr);
                    return(instance);
                });

                var convertedCollection = TypeHelper.ConvertCollection(relatedResources, attr.Type);

                attr.SetValue(entity, convertedCollection);
                _jsonApiContext.RelationshipsToUpdate[attr] = convertedCollection;
                _jsonApiContext.HasManyRelationshipPointers.Add(attr, convertedCollection);
            }

            return(entity);
        }
        /// <summary>
        /// Sets a HasMany relationship.
        /// </summary>
        private void SetHasManyRelationship(IIdentifiable resource, HasManyAttribute hasManyRelationship, RelationshipEntry relationshipData)
        {
            if (relationshipData.ManyData == null)
            {
                throw new JsonApiSerializationException("Expected data[] element for to-many relationship.",
                                                        $"Expected data[] element for '{hasManyRelationship.PublicName}' relationship.", atomicOperationIndex: AtomicOperationIndex);
            }

            HashSet <IIdentifiable> rightResources = relationshipData.ManyData.Select(rio => CreateRightResource(hasManyRelationship, rio))
                                                     .ToHashSet(IdentifiableComparer.Instance);

            IEnumerable convertedCollection = CollectionConverter.CopyToTypedCollection(rightResources, hasManyRelationship.Property.PropertyType);

            hasManyRelationship.SetValue(resource, convertedCollection);

            AfterProcessField(resource, hasManyRelationship, relationshipData);
        }
Пример #4
0
        /// <summary>
        /// Sets a HasMany relationship.
        /// </summary>
        private void SetHasManyRelationship(
            IIdentifiable entity,
            HasManyAttribute attr,
            RelationshipEntry relationshipData)
        {
            if (relationshipData.Data != null)
            {   // if the relationship is set to null, no need to set the navigation property to null: this is the default value.
                var relatedResources = relationshipData.ManyData.Select(rio =>
                {
                    var relatedInstance      = attr.RightType.New <IIdentifiable>();
                    relatedInstance.StringId = rio.Id;
                    return(relatedInstance);
                });
                var convertedCollection = relatedResources.Cast(attr.RightType);
                attr.SetValue(entity, convertedCollection);
            }

            AfterProcessField(entity, attr, relationshipData);
        }
Пример #5
0
        /// <summary>
        /// Sets a HasMany relationship.
        /// </summary>
        private void SetHasManyRelationship(
            IIdentifiable resource,
            HasManyAttribute attr,
            RelationshipEntry relationshipData)
        {
            if (relationshipData.Data != null)
            {   // if the relationship is set to null, no need to set the navigation property to null: this is the default value.
                var relatedResources = relationshipData.ManyData.Select(rio =>
                {
                    var relatedInstance      = (IIdentifiable)ResourceFactory.CreateInstance(attr.RightType);
                    relatedInstance.StringId = rio.Id;
                    return(relatedInstance);
                });

                var convertedCollection = TypeHelper.CopyToTypedCollection(relatedResources, attr.Property.PropertyType);
                attr.SetValue(resource, convertedCollection, ResourceFactory);
            }

            AfterProcessField(resource, attr, relationshipData);
        }