问题
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