gpt4 book ai didi

web-services - 使用 ServiceStack 实现 HATEOAS 的正确方法

转载 作者:行者123 更新时间:2023-12-02 01:50:00 25 4
gpt4 key购买 nike

know how mythz generally feels about HATEOAS , 但假设我必须遵循 HATEOAS我的 REST 服务中的原则,并向我的 DTO 添加链接(“ self ”、“ parent ”和其他可能的关系)。

“self”和“parent”等链接包含资源路径,这些路径当然与我的路线相关。

我正在为我的 ServiceStack REST 服务使用以下项目/部署结构。如果这很重要,我正在使用 ServiceStack 3.9.71

服务网关组件:

  • 定义我的 DTO。每个 DTO 都有一个从相应的域对象创建该 DTO 的工厂
  • 定义操作及其路线

服务实现组装:

  • 使用 ServiceGateway 获取 DTO 定义并访问其工厂
  • 做任何领域逻辑需要的事情,并通过上述工厂创建相应的 DTO

服务接口(interface)组装:

  • 定义我的 REST 服务和
  • 根据 REST 动词(GET、POST、...)从 ServiceStack 的 HttpHandler 调用 ServiceImplementation

哪里是将链接信息添加到我的 DTO 的合适位置?

选项 1:

In my Service Gateway, when I build the DTOs themselves. It seems logical: I know what I need to know about my domain objects and I can easily build the links. Except that my DTOs are now all including an additional member (Links) and building those links forces me to explicitly provide paths/routes (i.e. hard code them). Seems to lead to a maintenance nightmare.

选项 2:

In my Service Interface assembly, where I have the request context and I know my routes. I can encapsulate whatever my Service Implementation returns in a meta-object containing the response and a link collection. However, to build that link collection, I sometimes need information available at the domain (i.e. Service Implementation) level. The big "con" side for me is that it creates a new additional and artificial level in all my responses. Could be seen as a way to standardize response formats but I don't like it.

选项 3:

My hope is that I can write a wrapper generically "injecting" a "Links" member to all the DTOs I return by hooking somewhere into ServiceStack in my Service Interface assembly. I haven't investigated much in that direction because I feel I could be wrong on the whole approach here.

欢迎任何意见/建议。感谢大家。

最佳答案

我不确定我是在建议您选择选项 1 还是选项 3,但这是我在尝试做同样的事情后得出的结论。

我从this answer开始.

现在you can access route metadata directly from filters .

所以我目前的做法是:

=> 服务创建 dto 响应和将附加到响应的下一个超媒体链接集合。在此级别中,您按类型了解“操作”,但不知道“如何”构建路线。我认为领域级别处理操作工作流是连贯的。

=> 在响应过滤器中,我从元数据中获取可用路由,并按照约定从 dto 属性构建路由。最后将路由添加到 http header 。

注意事项:我正在映射 1 dto - 1 路线。在其他情况下,这种方法可能会更加困难。

关于web-services - 使用 ServiceStack 实现 HATEOAS 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23389595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com