public List <SingleResource> CreateIncludedRepresentations(List <object> primaryResourceList, IResourceMapping resourceMapping, Context context) { var includedList = new List <SingleResource>(); var primaryResourceIdentifiers = primaryResourceList.Select(x => { var id = new SingleResourceIdentifier { Id = resourceMapping.IdGetter(x).ToString(), Type = resourceMapping.ResourceType, MetaData = GetRelationshipMetadata(x) }; return(id); }); var alreadyVisitedObjects = new HashSet <SingleResourceIdentifier>(primaryResourceIdentifiers, new SingleResourceIdentifierComparer()); foreach (var resource in primaryResourceList) { includedList.AddRange( AppendIncludedRepresentationRecursive( resource, resourceMapping, alreadyVisitedObjects, context)); } if (includedList.Any()) { return(includedList); } return(null); }
private List <SingleResource> AppendIncludedRepresentationRecursive( object resource, IResourceMapping resourceMapping, HashSet <SingleResourceIdentifier> alreadyVisitedObjects, Context context, string parentRelationshipPath = "") { var includedResources = new List <SingleResource>(); foreach (var relationship in resourceMapping.Relationships) { if (relationship.InclusionRule == ResourceInclusionRules.ForceOmit) { continue; } var relatedResources = UnifyObjectsToList(relationship.RelatedResource(resource)); string relationshipPath = BuildRelationshipPath(parentRelationshipPath, relationship); if (!context.IncludedResources.Any(x => x.Contains(relationshipPath))) { continue; } foreach (var relatedResource in relatedResources) { var relatedResourceId = new SingleResourceIdentifier { Id = relationship.ResourceMapping.IdGetter(relatedResource).ToString(), Type = relationship.ResourceMapping.ResourceType, MetaData = GetRelationshipMetadata(relatedResource) }; if (alreadyVisitedObjects.Contains(relatedResourceId)) { continue; } alreadyVisitedObjects.Add(relatedResourceId); includedResources.Add( CreateResourceRepresentation(relatedResource, relationship.ResourceMapping, context)); includedResources.AddRange( AppendIncludedRepresentationRecursive(relatedResource, relationship.ResourceMapping, alreadyVisitedObjects, context, relationshipPath)); } } return(includedResources); }