Hypermedia links with Servicestack new API

核能气质少年 提交于 2019-11-30 20:16:08

The way I do this currently is I pass back a response dto which implements an interface

public interface IHaveLinks
{
  [IgnoreDataMember]
  IEnumerable<Link> Links { get; }
}

public class Link
{
  public string Name { get; set; }
  public IReturn Request { get; set; }
  public string Method { get; set; }
}

Then I use a response filter to generate the urls and populate the response headers with the links.

this.ResponseFilters.Add((req, res, dto) =>
{
  if (!(dto is IHaveLinks))
    return;

  var links = (dto as IHaveLinks).Links

  if(links == null || !links.Any())
    return;

  var linksText = links
    .Select(x => string.Format("<{0}>; rel={1}"), x.Request.ToUrl(x.Method), x.Name));

  var linkHeader = string.Join(", ", linksText);

  res.AddHeader("Link", linkHeader);
});

This seems the cleanest way. The Link object above effectively says "If you make this request with this method you will get back the named resource". The only HTTP thing that bleeds up to the BLL is Method. But you could get rid of that and only pass back GET urls. Or map it to some generalised "operation"?

As an example:

public class ExampleService : Service
{
  public ExamplesResponse Get(ExamplesRequest request)
  {
    var page = request.Page;
    var data = // get data;

    return new ExamplesResponse
      {
        Examples = data,
        Links = new []
          {
            new Link { Name = "next", Request = request.AddPage(1), Method = "GET" },
            new Link { Name = "previous", Request = request.AddPage(-1), Method = "GET" },
          }
      }
  }
}

[Route("/examples/{Page}")]
public class ExamplesRequest : IReturn<ExamplesResponse>
{
  public int Page { get; set; }

  // ...
}

(The AddPage method returns a clone of the request and sets the Page property appropriately.)

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!