// PRIVATE METHODS ////////////////////////////////////////////////// #region Methods private void AddResourceLinkage(IServiceModel serviceModel, Type fromClrResourceType, object fromClrResource, string fromApiRel, Type toClrResourceType, object toClrResource) { Contract.Requires(serviceModel != null); Contract.Requires(String.IsNullOrWhiteSpace(fromApiRel)); if (fromClrResourceType == null || fromClrResource == null) { return; } // Create ResourceLinkageKey from ToOneIncludedResource. var fromResourceType = serviceModel.GetResourceType(fromClrResourceType); var fromApiResourceIdentifier = fromResourceType.GetApiResourceIdentifier(fromClrResource); var apiResourceLinkageKey = new ApiResourceLinkageKey(fromApiResourceIdentifier, fromApiRel); // Create ResourceLinkage from ToOneIncludedResource var toApiResourceIdentifier = default(ResourceIdentifier); if (toClrResourceType != null && toClrResource != null) { var toResourceType = serviceModel.GetResourceType(toClrResourceType); toApiResourceIdentifier = toResourceType.GetApiResourceIdentifier(toClrResource); } var apiResourceLinkage = new ApiResourceLinkage(toApiResourceIdentifier); // Add ResourceLinkage to this DocumentBuilderContext this.AddResourceLinkage(apiResourceLinkageKey, apiResourceLinkage); }
private void AddResourceLinkage(IServiceModel serviceModel, Type fromClrResourceType, object fromClrResource, string fromApiRel, Type toClrResourceType, IEnumerable <object> toClrResourceCollection) { Contract.Requires(serviceModel != null); Contract.Requires(String.IsNullOrWhiteSpace(fromApiRel)); if (fromClrResourceType == null || fromClrResource == null) { return; } // Create ResourceLinkageKey from ToManyIncludedResources. var fromResourceType = serviceModel.GetResourceType(fromClrResourceType); var fromApiResourceIdentifier = fromResourceType.GetApiResourceIdentifier(fromClrResource); var apiResourceLinkageKey = new ApiResourceLinkageKey(fromApiResourceIdentifier, fromApiRel); // Create ResourceLinkage from ToManyIncludedResources. var toApiResourceIdentifierCollection = Enumerable.Empty <ResourceIdentifier>() .ToList(); if (toClrResourceType != null && toClrResourceCollection != null) { var toResourceType = serviceModel.GetResourceType(toClrResourceType); toApiResourceIdentifierCollection = toClrResourceCollection.Select(toResourceType.GetApiResourceIdentifier) .ToList(); } var apiResourceLinkage = new ApiResourceLinkage(toApiResourceIdentifierCollection); // Add ResourceLinkage to this DocumentBuilderContext this.AddResourceLinkage(apiResourceLinkageKey, apiResourceLinkage); }
// PRIVATE METHODS ////////////////////////////////////////////////// #region Methods private void AddDomReadWriteRelationshipIfNeeded(ApiResourceLinkageKey apiResourceLinkageKey) { // Add DOM read/write relationship node if not already added. var fromApiResourceIdentifier = apiResourceLinkageKey.FromResourceIdentifier; var fromApiRel = apiResourceLinkageKey.FromRel; if (!this.DomReadWriteResourceDictionary.TryGetValue(fromApiResourceIdentifier, out var domResource)) { return; } var domReadWriteRelationships = (DomReadWriteRelationships)domResource.GetNode(DomNodeType.Relationships); // ReSharper disable once ConvertIfStatementToNullCoalescingExpression if (domReadWriteRelationships == null) { // Add DOM read/write relationships node domReadWriteRelationships = domResource.GetOrAddNode(DomNodeType.Relationships, () => DomReadWriteRelationships.Create()); } var domReadWriteRelationshipExists = domReadWriteRelationships.Nodes() .Cast <IDomRelationship>() .Any(x => x.Rel == fromApiRel); if (domReadWriteRelationshipExists) { return; } // Add DOM read/write relationship node. domReadWriteRelationships.AddDomReadWriteRelationship(fromApiRel); }
private void AddResourceLinkage(ApiResourceLinkageKey apiResourceLinkageKey, ApiResourceLinkage apiResourceLinkageNew) { Contract.Requires(apiResourceLinkageKey != null); Contract.Requires(apiResourceLinkageNew != null); this.AddDomReadWriteRelationshipIfNeeded(apiResourceLinkageKey); if (this.ApiResourceLinkageDictionary.TryGetValue(apiResourceLinkageKey, out var apiResourceLinkageExisting) == false) { // Add initial new resource linkage this.ApiResourceLinkageDictionary.Add(apiResourceLinkageKey, apiResourceLinkageNew); return; } // Merge existing and new resource linkage var apiResourceLinkageExistingType = apiResourceLinkageExisting.Type; switch (apiResourceLinkageExistingType) { case ApiResourceLinkageType.ToOneResourceLinkage: { this.ApiResourceLinkageDictionary.Remove(apiResourceLinkageKey); this.ApiResourceLinkageDictionary.Add(apiResourceLinkageKey, apiResourceLinkageNew); break; } case ApiResourceLinkageType.ToManyResourceLinkage: { var toManyResourceLinkage = apiResourceLinkageExisting.ToManyResourceLinkage .SafeToList(); toManyResourceLinkage.AddRange(apiResourceLinkageNew.ToManyResourceLinkage); toManyResourceLinkage = toManyResourceLinkage.Distinct() .SafeToList(); var apiResourceLinkageMerged = new ApiResourceLinkage(toManyResourceLinkage); this.ApiResourceLinkageDictionary.Remove(apiResourceLinkageKey); this.ApiResourceLinkageDictionary.Add(apiResourceLinkageKey, apiResourceLinkageMerged); break; } default: throw new ArgumentOutOfRangeException(); } }
public void AddResourceLinkage <TFromResource, TToResource>(IServiceModel serviceModel, IToManyIncludedResources <TFromResource, TToResource> toManyIncludedResources) where TFromResource : class, IResource where TToResource : class, IResource { Contract.Requires(serviceModel != null); Contract.Requires(toManyIncludedResources != null); // Create ResourceLinkageKey from ToManyIncludedResources. var fromClrResourceType = typeof(TFromResource); var fromResourceType = serviceModel.GetResourceType(fromClrResourceType); var fromClrResource = toManyIncludedResources.FromResource; var fromApiResourceIdentifier = fromResourceType.GetApiResourceIdentifier(fromClrResource); var fromApiRel = toManyIncludedResources.FromRel; var apiResourceLinkageKey = new ApiResourceLinkageKey(fromApiResourceIdentifier, fromApiRel); // Create ResourceLinkage from ToManyIncludedResources. var toApiResourceIdentifierCollection = Enumerable.Empty <ResourceIdentifier>() .ToList(); var toClrResourceCollection = toManyIncludedResources.ToResourceCollection; if (toClrResourceCollection != null) { var toClrResourceType = typeof(TToResource); var toResourceType = serviceModel.GetResourceType(toClrResourceType); toApiResourceIdentifierCollection = toClrResourceCollection.Select(toResourceType.GetApiResourceIdentifier) .ToList(); } var apiResourceLinkage = new ApiResourceLinkage(toApiResourceIdentifierCollection); // Add ResourceLinkage to this DocumentBuilderContext this.AddResourceLinkage(apiResourceLinkageKey, apiResourceLinkage); }
public void AddResourceLinkage <TFromResource, TToResource>(IServiceModel serviceModel, IToOneIncludedResource <TFromResource, TToResource> toOneIncludedResource) where TFromResource : class, IResource where TToResource : class, IResource { Contract.Requires(serviceModel != null); Contract.Requires(toOneIncludedResource != null); // Create ResourceLinkageKey from ToOneIncludedResource. var fromClrResourceType = typeof(TFromResource); var fromResourceType = serviceModel.GetResourceType(fromClrResourceType); var fromClrResource = toOneIncludedResource.FromResource; var fromApiResourceIdentifier = fromResourceType.GetApiResourceIdentifier(fromClrResource); var fromApiRel = toOneIncludedResource.FromRel; var apiResourceLinkageKey = new ApiResourceLinkageKey(fromApiResourceIdentifier, fromApiRel); // Create ResourceLinkage from ToOneIncludedResource var toApiResourceIdentifier = default(ResourceIdentifier); var toClrResource = toOneIncludedResource.ToResource; if (toClrResource != null) { var toClrResourceType = typeof(TToResource); var toResourceType = serviceModel.GetResourceType(toClrResourceType); toApiResourceIdentifier = toResourceType.GetApiResourceIdentifier(toClrResource); } var apiResourceLinkage = new ApiResourceLinkage(toApiResourceIdentifier); // Add ResourceLinkage to this DocumentBuilderContext this.AddResourceLinkage(apiResourceLinkageKey, apiResourceLinkage); }
public bool TryGetResourceLinkage(ApiResourceLinkageKey apiResourceLinkageKey, out ApiResourceLinkage apiResourceLinkage) { return(this.ApiResourceLinkageDictionary.TryGetValue(apiResourceLinkageKey, out apiResourceLinkage)); }