Aggregate pattern
Aggregates are probably the most difficult building blocks of Domain-Driven Design1. They're hard to understand, and they're even harder to properly design. Aggregates are Entities that hold other Entities and Value Objects that help keep data consistent.
Pramod J. Sadalage and Martin Fowler definition
In Domain-Driven Design, an aggregate is a collection of related objects that we wish to treat as a unit. In particular, it is a unit for data manipulation and management of consistency. Typically, we like to update aggregates with atomic operations and communicate with our data storage in terms of aggregates.2
Martin Fowler paper
Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects that can be treated as a single unit. An example may be an order and its line-items, these will be separate objects, but it is useful to treat the order (together with its line items) as...3
What does an Aggregate look like ?
An aggregate is made up of two main parts namely a boundary (B) and a root (AR).
The boundary defines the contents of the aggregate. It is also the barrier between the aggregate contents and the rest of the application. Nothing outside the boundary can keep a reference to anything inside the boundary.
The Aggregate Root (AR) provides access to the aggregate contents. It is the only object in the aggregate that objects outside the aggregate may hold a reference to. The aggregate root in turn holds references to the aggregate contents.It provides access to the contents, but it is not direct access. We will not ask the root for an object and then do work on the object. We would ask the aggregate (A) to do the work on the object for us. In-fact we might be oblivious of the objects existence. The contents of an aggregate is made up of our basic DDD building blocks: Entities (E), Value Objects (VO) and other Aggregates (A).
A single Entity without any child Entities or Value Objects is also an Aggregate by itself. The main goal of an Aggregate is to keep your Domain Model consistent. Aggregates centralize most of the business rules. Aggregates are persisted atomically in your persistence mechanism. No matter how many child Entities and Value Objects live inside the root Entity, all of them will be persisted atomically, as a single unit.
1. “Domain-driven Design”, This content ↩
2. “NoSQL Distilled”, Pramod J. Sadalage and Martin Fowler, https://www.amazon.com/NoSQL-Distilled-Emerging-Polyglot-Persistence/dp/0321826620 ↩
3. “DDD Aggregate”, Martin Fowler, https://martinfowler.com/bliki/DDD_Aggregate.html ↩