private static void AddPath(IResourceType resourceType,
                                    Type clrResourceType,
                                    string apiId,
                                    IRelationshipInfo nextRelationship,
                                    ref List <IHypermediaPath> resourceBasePath,
                                    ref ResourcePathMode resourcePathMode,
                                    ref IRelationshipInfo resourcePreviousRelationship)
        {
            Contract.Requires(resourceType != null);
            Contract.Requires(clrResourceType != null);
            Contract.Requires(String.IsNullOrWhiteSpace(apiId) == false);
            Contract.Requires(nextRelationship != null);
            Contract.Requires(resourceBasePath != null);

            if (resourcePreviousRelationship == null)
            {
                var apiCollectionPathSegment = resourceType.HypermediaInfo.ApiCollectionPathSegment;

                var hypermediaPath = default(IHypermediaPath);
                if (!resourceType.IsSingleton())
                {
                    hypermediaPath   = new ResourceHypermediaPath(clrResourceType, apiCollectionPathSegment, apiId);
                    resourcePathMode = ResourcePathMode.IncludeApiId;
                }
                else
                {
                    hypermediaPath   = new SingletonHypermediaPath(clrResourceType, apiCollectionPathSegment);
                    resourcePathMode = ResourcePathMode.IgnoreApiId;
                }

                resourceBasePath.Add(hypermediaPath);
                resourcePreviousRelationship = nextRelationship;
                return;
            }

            var previousRelationshipCardinality = resourcePreviousRelationship.ToCardinality;
            var apiRelationshipRelPathSegment   = resourcePreviousRelationship.ApiRelPathSegment;

            switch (previousRelationshipCardinality)
            {
            case RelationshipCardinality.ToOne:
            {
                var toOneResourceHypermediaPath = new ToOneResourceHypermediaPath(clrResourceType, apiRelationshipRelPathSegment);

                resourceBasePath.Add(toOneResourceHypermediaPath);
                resourcePathMode = ResourcePathMode.IgnoreApiId;
            }
            break;

            case RelationshipCardinality.ToMany:
            {
                var toManyResourceHypermediaPath = new ToManyResourceHypermediaPath(clrResourceType, apiRelationshipRelPathSegment, apiId);

                resourceBasePath.Add(toManyResourceHypermediaPath);
                resourcePathMode = ResourcePathMode.IncludeApiId;
            }
            break;

            default:
            {
                var detail = InfrastructureErrorStrings.InternalErrorExceptionDetailUnknownEnumerationValue
                             .FormatWith(typeof(RelationshipCardinality).Name, previousRelationshipCardinality);
                throw new InternalErrorException(detail);
            }
            }

            resourcePreviousRelationship = nextRelationship;
        }
Example #2
0
        // PRIVATE METHODS //////////////////////////////////////////////////
        #region Methods
        private static IEnumerable <string> GetResourcePath(IEnumerable <IHypermediaPath> resourceBasePath, ResourcePathMode resourcePathMode, string apiId)
        {
            Contract.Requires(resourceBasePath != null);
            Contract.Requires(String.IsNullOrWhiteSpace(apiId) == false);

            var resourcePath = resourceBasePath.SelectMany(x => x.PathSegments).ToList();

            switch (resourcePathMode)
            {
            case ResourcePathMode.IncludeApiId:
            {
                resourcePath.Add(apiId);
            }
            break;

            case ResourcePathMode.IgnoreApiId:
            {
                // NOOP
            }
            break;

            default:
            {
                var detail = InfrastructureErrorStrings.InternalErrorExceptionDetailUnknownEnumerationValue
                             .FormatWith(typeof(ResourcePathMode).Name, resourcePathMode);
                throw new InternalErrorException(detail);
            }
            }

            return(resourcePath);
        }
        public ResourcePathContext(IEnumerable <IHypermediaPath> resourceSelfBasePath, ResourcePathMode resourceSelfPathMode,
                                   IEnumerable <IHypermediaPath> resourceCanonicalBasePath, ResourcePathMode resourceCanonicalPathMode)
        {
            Contract.Requires(resourceSelfBasePath != null);
            Contract.Requires(resourceCanonicalBasePath != null);

            this.ResourceSelfBasePath = resourceSelfBasePath;
            this.ResourceSelfPathMode = resourceSelfPathMode;

            this.ResourceCanonicalBasePath = resourceCanonicalBasePath;
            this.ResourceCanonicalPathMode = resourceCanonicalPathMode;
        }