超媒体与Servicestack新的API链接 [英] Hypermedia links with Servicestack new API

查看:129
本文介绍了超媒体与Servicestack新的API链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估如何在超媒体链接添加到DTO的响应。虽然没有标准,添加列表到响应的DTO似乎是的建议的方法

I am evaluating how to add hypermedia links to DTO responses. Although there is no standard, add List to the response DTOs seems to be the suggested approach.

你知道的任何例子或者使用ServiceStack框架实现的参考?

Do you know of any example or reference of implementation using ServiceStack framework?

添加名单是确定的我,但我的疑惑是在哪里把下面的链接(在服务或保存状态机的专业类的逻辑是什么? ),并在解决路由(过滤器?)

Adding List is ok for me, but my doubts are about where to put the logic of the following links (Within the service or a specialized class that holds the state machine?) and where to resolve the routes (A filter?)

感谢。

[更新]从ServiceStack版本v3.9.62它是更多钞票通过EndpointHost.Config.Metadata.Routes.RestPath访问路由配置,因此通过 tgmdbm 可以withouth的的需要IReturn +路由属性,只是用Metadata.Routes信息加以改进。
事实上,所有服务元数据可以查询和使用横切关注点。 Servicestack岩石。

[Update] From ServiceStack version v3.9.62 it is posible to access Routes configuration via EndpointHost.Config.Metadata.Routes.RestPath, so the solution provided by tgmdbm can be improved withouth the need of "IReturn + Routes attributes", just using Metadata.Routes information. In fact all service metadata can be queried and used to cross-cutting concerns. Servicestack rocks.

推荐答案

我这个问题的方法是目前我传回DTO它实现一个接口的响应

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);
});

这似乎是最彻底的方法。在链接对象之上的有效写着如果你用这种方法你会得到命名资源这一要求。即出血达BLL唯一的HTTP就是方法。但你可以摆脱这一点,只能通过后退的URL。或者,它映射到一些广义的操作

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"?

作为一个例子:

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; }

  // ...
}



(本 AddPage 方法返回请求的克隆,并适当设置页面属性。)

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

希望有所帮助。

这篇关于超媒体与Servicestack新的API链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆