Esempio n. 1
0
        public object GetHypermediaRouteKeys(HypermediaObjectReferenceBase reference)
        {
            if (!this.routeRegister.TryGetKeyProducer(reference.GetHypermediaType(), out var keyProducer))
            {
                return(new { });
            }

            var key = reference.GetKey(keyProducer);

            if (key == null)
            {
                return(new { });
            }

            return(key);
        }
        public string ReferenceToRoute(HypermediaObjectReferenceBase reference)
        {
            var lookupType = reference.GetHypermediaType();

            // ExternalReference object is not registered in the RouteRegister and provides its own URI
            if (typeof(ExternalReference).IsAssignableFrom(lookupType))
            {
                var externalReferenceObject = reference.GetInstance() as ExternalReference;
                if (externalReferenceObject == null)
                {
                    throw new HypermediaRouteException("Can not get instance for ExternalReference.");
                }

                return(externalReferenceObject.ExternalUri.ToString());
            }

            var routeKeys = this.routeKeyFactory.GetHypermediaRouteKeys(reference);

            return(this.GetRouteByType(lookupType, routeKeys));
        }
        public ResolvedRoute ReferenceToRoute(HypermediaObjectReferenceBase reference)
        {
            var lookupType = reference.GetHypermediaType();

            // ExternalReference object is not registered in the RouteRegister and provides its own URI

            if (externalReferenceTypeInfo.IsAssignableFrom(lookupType))
            {
                if (!(reference.GetInstance() is ExternalReference externalReferenceObject))
                {
                    throw new HypermediaRouteException("Can not get instance for ExternalReference.");
                }

                // we assume get here since external references will be links only for now
                return(new ResolvedRoute(externalReferenceObject.ExternalUri.ToString(), HttpMethod.GET, externalReferenceObject.AvailableMediaTypes));
            }

            if (internalReferenceTypeInfo.IsAssignableFrom(lookupType))
            {
                if (!(reference.GetInstance() is InternalReference internalReference))
                {
                    throw new HypermediaRouteException("Can not get instance for InternalReference.");
                }

                // we assume get here since external references will be links only for now
                var routeInfo             = new RouteInfo(internalReference.RouteName, HttpMethod.GET);
                var resolvedInternalRoute = RouteUrl(routeInfo, internalReference.RouteParameters);
                resolvedInternalRoute.AvailableMediaTypes = internalReference.AvailableMediaTypes;
                return(resolvedInternalRoute);
            }

            if (reference is HypermediaExternalObjectReference)
            {
                throw new HypermediaRouteException("Can not get instance for HypermediaExternalObjectReference.");
            }

            var routeKeys = this.routeKeyFactory.GetHypermediaRouteKeys(reference);

            return(this.GetRouteByType(lookupType, routeKeys));
        }