DDD Which is the root Aggregate root?

北城以北 提交于 2019-12-10 17:55:41

问题


Lots of examples like order and order lines makes sense, like:

Order is an AR that contains OrderLines Customer is an AR that contains Orders.

Question is, what is the AR that contains Customer? I guess it can be something like "shop".

So, shop.AddCustomer(customer)...

but, how to get shop? If it's an AR (entity) it has an id, so shop.GetById(shopId). If I only have one shop, how does this work with persistence?

Should I have a table (shops) with one line? Shop is an in-memory object with a collection of Customers?


回答1:


You got that wrong there. Aggregates do not contain other aggregates! They can only reference them by ID.

An aggregate is a group of entities and value objects that are closely related. The aggregate forms a consistency boundary around them. The Aggregate Root is the root entity in that aggregate that is globally addressable. So in your example with Order and OrderLines, Order could indeed be the AR.

Customer on the other hand, would only reference Orders by ID if it is a separate aggregate.

To retrieve an aggregate, you typically use a Repository. You load an aggregate through the repository by specifying the ID of the aggregate, or some other suitable search parameter.



来源:https://stackoverflow.com/questions/34423613/ddd-which-is-the-root-aggregate-root

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