Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you persist/restore aggregate roots with entities in DDD?

Based on the following definitions from Domain-Driven Design: Tackling Complexity in the Heart of Software,

An aggregate is: A cluster of associated objects that are treated as a unit for the purpose of data changes. External references are restricted to one member of the AGGREGATE, designated as the root. A set of consistency rules applies within the AGGREGATE'S boundaries.

I don't think the Aggregate root should hold a reference to the repository. Since the Aggregate root is the only one that should be holding references to its entities and aggregates, they should be private.

How can my repository persist and restore this private data ?


Edit:

Let's take the classic Order, OrderLines example.

An order is the Aggregate root.

It's lines are Entities.

Since the Aggregate root(order) is the only object allowed to hold references to its entities (order lines), I do not understand how would I persist order lines from the repository.

like image 839
Martin Avatar asked Feb 28 '11 16:02

Martin


People also ask

Is aggregate root an entity?

Thus, the aggregate root must be an entity, not a value object, so that it can be persisted to and from a data store using its ID.

What is aggregate root in DDD?

An aggregate root is a class which works as an entry point to our aggregate. All business operations should go through the root. This way, the aggregate root can take care of keeping the aggregate in a consistent state. The root is what takes cares of all our business invariants.

How do you choose the aggregate root?

When choosing an aggregate root you choose between Transactional Consistency and Eventual Consistency. When your business rules allow you would rather favour Eventual Consistency.

What's an aggregate root?

An Aggregate is the clump of related entities to treat as a unit for data changes. The Aggregate Root is the main entity that holds references to the other ones. It's the only entity in the clump that is used for direct lookup.


1 Answers

As far as I understand the aggregate root, it must be the place to access all the entities inside it's scope. That means, as long as traditional ORM is used, that you can access the OrderLines throug the Order.

Further it is not forbidden for anyone to grab a reference to the entitiy inside the root, but these references must be volatile (i.e. short lived) and you must obtain the rerefence via the aggregate root.

In terms of DDD you will use a repository to hide data access, the factory might in turn use a factory to assemble the object. The facotry knows well about the internal structure of the object and must be able to build up a new object or restore one from the data the repository hands over.

Perhaps you might also look into CQRS + Event Sourcing which provides a different approach to persisting entities.

like image 158
Tobias Avatar answered Oct 03 '22 22:10

Tobias