简体   繁体   English

聚合根在 REST API (DDD) 中的作用

[英]The role of Aggregate roots in a REST API (DDD)

I'm creating a new REST/hypermedia API for an on line auction service.我正在为在线拍卖服务创建一个新的 REST/超媒体 API。

I'm using this as an exercise to better understand Domain Driven Design approach as for the most part it seems like a good approach.我将此作为练习以更好地理解领域驱动设计方法,因为在大多数情况下它似乎是一种很好的方法。

An example of some of my entities are: Item, Listing, Bid, Purchase, BidHistory etc. I identify the Listing entity as an aggregate root through which I plan to manage Bid, Item etc.我的一些实体的示例是:Item、Listing、Bid、Purchase、BidHistory 等。我将 Listing 实体标识为一个聚合根,我计划通过它来管理 Bid、Item 等。

As far as I can tell, the concept of the aggregate root is applicable to my persistence/domain layer and should NOT be a concern of my view layer (in my case JSON or XML resource representations).据我所知,聚合根的概念适用于我的持久性/域层,不应该是我的视图层的问题(在我的例子中是 JSON 或 XML 资源表示)。

Is this the case?是这种情况吗? And if so, would this mean that it is still okay to expose non aggregate root resources via URI endpoints in my REST API or am I 'constrained' to only exposing aggregate roots through my API's endpoint?如果是这样,这是否意味着通过 REST API 中的 URI 端点公开非聚合根资源仍然可以,或者我是否“被限制”为仅通过我的 API 端点公开聚合根?

My thought is that aggregate roots are in the realm of the persistence object which is conceptually separate from the domain model and so I should be able to expose both URI such as:我的想法是聚合根位于持久性对象的领域中,它在概念上与域模型分离,因此我应该能够公开两个 URI,例如:

GET /api/v1/listing/465489

and

GET /api/v1/listing/465489/item

regardless of whether or not Listing is the aggregate root of Item or not.不管 Listing 是否是 Item 的聚合根。

Am I on the right lines here or do I need to adjust my understanding of this before I begin to implement any code?我在这里是正确的还是需要在开始实现任何代码之前调整我对此的理解?

I'm using this as an exercise to better understand Domain Driven Design approach as for the most part it seems like a good approach.我将此作为练习以更好地理解领域驱动设计方法,因为在大多数情况下它似乎是一种很好的方法。

OK... but they are orthogonal concerns, so be careful.好的...但它们是正交的关注点,所以要小心。

As far as I can tell, the concept of the aggregate root is applicable to my persistence/domain layer and should NOT be a concern of my view layer (in my case JSON or XML resource representations).据我所知,聚合根的概念适用于我的持久性/域层,不应该是我的视图层的问题(在我的例子中是 JSON 或 XML 资源表示)。

That's right - aggregates are a partitioning of your business domain.没错 - 聚合是您业务领域的分区。 Resources are part of your integration domain.资源是集成域的一部分。 See the Jim Webber talk REST - DDD in the Large .请参阅大中的 Jim Webber 谈话REST-DDD

would this mean that it is still okay to expose non aggregate root resources via URI endpoints in my REST API or am I 'constrained' to only exposing aggregate roots through my API's endpoint?这是否意味着通过 REST API 中的 URI 端点公开非聚合根资源仍然可以,或者我是否“被限制”为仅通过我的 API 端点公开聚合根?

It depends how you mean that.这取决于你的意思。 You aren't "exposing" your aggregate roots in any case, you are adapting your domain model for the web.在任何情况下,您都不会“暴露”聚合根,而是在为 Web调整域模型。 Which means your clients are manipulating resources, and as a side effect of this the resources manipulate the domain model.这意味着您的客户端正在操纵资源,并且作为此的副作用,资源操纵域模型。

So the messages that you send to the domain model from the api are still going to be addressed to the aggregate root, and further traversal will be required to get to the non root entities.因此,您从 api 发送到域模型的消息仍将寻址到聚合根,并且需要进一步遍历才能到达非根实体。

For operations that are safe , you don't really need the aggregate roots at all.对于安全的操作,您根本不需要聚合根。 The CQRS pattern is built on this idea; CQRS模式就是建立在这个想法之上的; that you can read a previously cached representation of the state of the model without risking the business invariant.您可以读取先前缓存的模型状态表示,而不会冒业务不变量的风险。 So creating resources with immutable semantics that provide access to entities in your domain is fine.因此,创建具有不可变语义的资源以提供对域中实体的访问是很好的。

Unsafe operations, however, are trickier -- you need to take the semantics of the uniform interface that you expose, and translate that into the appropriate message(s) to send to the aggregate root - because it is behind the root that the authority for validating the changes actually lives.然而,不安全的操作更棘手——你需要获取你公开的统一接口的语义,并将其转换为适当的消息以发送到聚合根——因为它的权限在根后面验证更改实际上是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM