Bart Calixto
Bart Calixto

Reputation: 19725

DDD Which is the root Aggregate root?

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?

Upvotes: 5

Views: 1080

Answers (1)

theDmi
theDmi

Reputation: 18034

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.

Upvotes: 8

Related Questions