// PRIVATE METHODS //////////////////////////////////////////////////
        #region Methods
        private void AddResourceLinkage(ResourceLinkageKey resourceLinkageKey, ResourceLinkage resourceLinkageNew)
        {
            Contract.Requires(resourceLinkageKey != null);
            Contract.Requires(resourceLinkageNew != null);

            ResourceLinkage resourceLinkageExisting;

            if (this.ResourceLinkageDictionary.TryGetValue(resourceLinkageKey, out resourceLinkageExisting) == false)
            {
                // Add initial new resource linkage
                this.ResourceLinkageDictionary.Add(resourceLinkageKey, resourceLinkageNew);
                return;
            }

            // Merge existing and new resource linkage
            var resourceLinkageExistingType = resourceLinkageExisting.Type;

            switch (resourceLinkageExistingType)
            {
            case ResourceLinkageType.ToOneResourceLinkage:
            {
                this.ResourceLinkageDictionary.Remove(resourceLinkageKey);
                this.ResourceLinkageDictionary.Add(resourceLinkageKey, resourceLinkageNew);
            }
            break;

            case ResourceLinkageType.ToManyResourceLinkage:
            {
                var toManyResourceLinkage = resourceLinkageExisting.ToManyResourceLinkage
                                            .SafeToList();
                toManyResourceLinkage.AddRange(resourceLinkageNew.ToManyResourceLinkage);

                toManyResourceLinkage = toManyResourceLinkage.Distinct()
                                        .SafeToList();

                var resourceLinkageMerged = new ResourceLinkage(toManyResourceLinkage);

                this.ResourceLinkageDictionary.Remove(resourceLinkageKey);
                this.ResourceLinkageDictionary.Add(resourceLinkageKey, resourceLinkageMerged);
            }
            break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public void AddResourceLinkage <TFromResource, TToResource>(IServiceModel serviceModel, IToManyResourceLinkage <TFromResource, TToResource> toManyResourceLinkage)
            where TFromResource : class, IResource
            where TToResource : class, IResource
        {
            Contract.Requires(serviceModel != null);
            Contract.Requires(toManyResourceLinkage != null);

            // Create ResourceLinkageKey from ToManyResourceLinkage.
            var fromClrResourceType = typeof(TFromResource);
            var fromResourceType    = serviceModel.GetResourceType(fromClrResourceType);

            var fromClrResource           = toManyResourceLinkage.FromResource;
            var fromApiResourceIdentifier = fromResourceType.GetApiResourceIdentifier(fromClrResource);

            var fromApiRel = toManyResourceLinkage.FromRel;

            var resourceLinkageKey = new ResourceLinkageKey(fromApiResourceIdentifier, fromApiRel);

            // Create ResourceLinkage from ToManyResourceLinkage.
            var toApiResourceIdentifierCollection = Enumerable.Empty <ResourceIdentifier>()
                                                    .ToList();

            var toClrResourceCollection = toManyResourceLinkage.ToResourceCollection;

            if (toClrResourceCollection != null)
            {
                var toClrResourceType = typeof(TToResource);
                var toResourceType    = serviceModel.GetResourceType(toClrResourceType);

                toApiResourceIdentifierCollection = toClrResourceCollection.Select(toResourceType.GetApiResourceIdentifier)
                                                    .ToList();
            }

            var resourceLinkage = new ResourceLinkage(toApiResourceIdentifierCollection);

            // Add ResourceLinkage to this DocumentBuilderContext
            this.AddResourceLinkage(resourceLinkageKey, resourceLinkage);
        }
        public void AddResourceLinkage <TFromResource, TToResource>(IServiceModel serviceModel, IToOneResourceLinkage <TFromResource, TToResource> toOneResourceLinkage)
            where TFromResource : class, IResource
            where TToResource : class, IResource
        {
            Contract.Requires(serviceModel != null);
            Contract.Requires(toOneResourceLinkage != null);

            // Create ResourceLinkageKey from ToOneResourceLinkage.
            var fromClrResourceType = typeof(TFromResource);
            var fromResourceType    = serviceModel.GetResourceType(fromClrResourceType);

            var fromClrResource           = toOneResourceLinkage.FromResource;
            var fromApiResourceIdentifier = fromResourceType.GetApiResourceIdentifier(fromClrResource);

            var fromApiRel = toOneResourceLinkage.FromRel;

            var resourceLinkageKey = new ResourceLinkageKey(fromApiResourceIdentifier, fromApiRel);

            // Create ResourceLinkage from ToOneResourceLinkage
            var toApiResourceIdentifier = default(ResourceIdentifier);

            var toClrResource = toOneResourceLinkage.ToResource;

            if (toClrResource != null)
            {
                var toClrResourceType = typeof(TToResource);
                var toResourceType    = serviceModel.GetResourceType(toClrResourceType);

                toApiResourceIdentifier = toResourceType.GetApiResourceIdentifier(toClrResource);
            }

            var resourceLinkage = new ResourceLinkage(toApiResourceIdentifier);

            // Add ResourceLinkage to this DocumentBuilderContext
            this.AddResourceLinkage(resourceLinkageKey, resourceLinkage);
        }
 public bool TryGetResourceLinkage(ResourceLinkageKey resourceLinkageKey, out ResourceLinkage resourceLinkage)
 {
     return(this.ResourceLinkageDictionary.TryGetValue(resourceLinkageKey, out resourceLinkage));
 }