/// <summary> /// Sets the principal side of a HasOne relationship, which means no /// foreign key is involved /// </summary> private void SetNavigation(IIdentifiable entity, HasOneAttribute attr, string relatedId) { if (relatedId == null) { attr.SetValue(entity, null, _resourceFactory); } else { var relatedInstance = (IIdentifiable)_resourceFactory.CreateInstance(attr.RightType); relatedInstance.StringId = relatedId; attr.SetValue(entity, relatedInstance, _resourceFactory); } }
/// <summary> /// Sets the principal side of a HasOne relationship, which means no /// foreign key is involved /// </summary> private void SetNavigation(IIdentifiable entity, HasOneAttribute attr, string relatedId) { if (relatedId == null) { attr.SetValue(entity, null); } else { var relatedInstance = attr.RightType.New <IIdentifiable>(); relatedInstance.StringId = relatedId; attr.SetValue(entity, relatedInstance); } }
/// <summary> /// Sets the principal side of a HasOne relationship, which means no /// foreign key is involved. /// </summary> private void SetNavigation(IIdentifiable resource, HasOneAttribute attr, string relatedId, Type relationshipType) { if (relatedId == null) { attr.SetValue(resource, null, ResourceFactory); } else { var relatedInstance = (IIdentifiable)ResourceFactory.CreateInstance(relationshipType); relatedInstance.StringId = relatedId; attr.SetValue(resource, relatedInstance, ResourceFactory); } }
/// <summary> /// Sets a HasOne relationship on a parsed resource. /// </summary> private void SetHasOneRelationship(IIdentifiable resource, HasOneAttribute hasOneRelationship, RelationshipEntry relationshipData) { if (relationshipData.ManyData != null) { throw new JsonApiSerializationException("Expected single data element for to-one relationship.", $"Expected single data element for '{hasOneRelationship.PublicName}' relationship."); } var rightResource = CreateRightResource(hasOneRelationship, relationshipData.SingleData); hasOneRelationship.SetValue(resource, rightResource); // depending on if this base parser is used client-side or server-side, // different additional processing per field needs to be executed. AfterProcessField(resource, hasOneRelationship, relationshipData); }
/// <summary> /// Sets the value of the navigation property for the related resource. /// If the resource has been included, all attributes will be set. /// If the resource has not been included, only the id will be set. /// </summary> private void SetHasOneNavigationPropertyValue(object entity, HasOneAttribute hasOneAttr, ResourceIdentifierObject rio, List <DocumentData> included) { // if the resource identifier is null, there should be no reason to instantiate an instance if (rio != null && rio.Id != null) { // we have now set the FK property on the resource, now we need to check to see if the // related entity was included in the payload and update its attributes var includedRelationshipObject = GetIncludedRelationship(rio, included, hasOneAttr); if (includedRelationshipObject != null) { hasOneAttr.SetValue(entity, includedRelationshipObject); } // we need to store the fact that this relationship was included in the payload // for EF, the repository will use these pointers to make ensure we don't try to // create resources if they already exist, we just need to create the relationship _jsonApiContext.HasOneRelationshipPointers.Add(hasOneAttr, includedRelationshipObject); } }
/// <summary> /// Sets the value of the navigation property for the related resource. /// If the resource has been included, all attributes will be set. /// If the resource has not been included, only the id will be set. /// </summary> private void SetHasOneNavigationPropertyValue(object entity, HasOneAttribute hasOneAttr, ResourceIdentifierObject rio, List <ResourceObject> included) { // if the resource identifier is null, there should be no reason to instantiate an instance if (rio != null && rio.Id != null) { // we have now set the FK property on the resource, now we need to check to see if the // related entity was included in the payload and update its attributes var includedRelationshipObject = GetIncludedRelationship(rio, included, hasOneAttr); if (includedRelationshipObject != null) { hasOneAttr.SetValue(entity, includedRelationshipObject); } /// 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.HasOneRelationshipPointers.Add(hasOneAttr, null); } }