Example #1
0
        public static Uri GetHref(this LinkFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var baseUrl = context.GetBaseUrl();
            var urlPath = context.GetUrlPath();

            if (baseUrl == null && urlPath == null)
            {
                return(null);
            }

            if (baseUrl == null)
            {
                return(urlPath);
            }

            if (urlPath == null)
            {
                return(baseUrl);
            }

            Uri.TryCreate(baseUrl, urlPath, out Uri absoluteUrl);

            return(absoluteUrl);
        }
Example #2
0
        public static Uri GetBaseUrl(this LinkFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(context.Get(LinkFactoryContext.BaseUrlKey) as Uri);
        }
Example #3
0
        public Link Create(LinkFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            return(DoCreate(context));
        }
Example #4
0
        public static LinkFactoryContext SetUrlPath(this LinkFactoryContext context, Uri value)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            context.Set(LinkFactoryContext.UrlPathKey, value);

            return(context);
        }
Example #5
0
        public static LinkMapper <TLink> MapTo <TLink>(this LinkFactoryContext context, Func <Uri, TLink> createHandler)
            where TLink : Link
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (createHandler == null)
            {
                throw new ArgumentNullException(nameof(createHandler));
            }

            var link = createHandler.Invoke(context.GetHref());

            return(new LinkMapper <TLink>(context, link));
        }
Example #6
0
 protected virtual Link DoCreate(LinkFactoryContext context)
 {
     return(context.MapTo(h => new Link(h.ToString())).Link);
 }
 public LinkMapper(LinkFactoryContext context, TLink link)
 {
     Context = context ?? throw new ArgumentNullException(nameof(context));
     Link    = link ?? throw new ArgumentNullException(nameof(link));
 }