Aggregate pattern in Domain-Driven Design

Alexey Zimarev
Eventuous
Published in
4 min readDec 14, 2022

--

Flock of starlings in shape of a large bird

The Aggregate pattern is probably the most important tactical pattern in Domain-Driven Design (DDD). In essence, it could be described like this:

  1. An aggregate is a cluster of domain objects that can be treated as a single unit. This means that the objects within an aggregate are closely related and are treated as a unit for the purpose of data changes.
  2. An aggregate has a root entity, which is the main object in the aggregate. The root entity is responsible for enforcing the invariants of the aggregate, which ensure the integrity of the data within the aggregate.
  3. An aggregate has a boundary, defining what objects are part of the aggregate and which are not. This boundary helps to ensure that objects outside of the aggregate cannot directly access the objects within the aggregate.
  4. An aggregate is consistency-maintaining. This means that the root entity is responsible for ensuring that the objects within the aggregate are always in a consistent state. Any changes to the objects within the aggregate must be made through the root entity, and the root entity must ensure that the changes do not violate the invariants of the aggregate.
  5. An aggregate is transactional. This means that all the changes to the objects within an aggregate are treated as a single unit of work, and either all the changes are applied, or none of the changes are applied. This ensures that the aggregate remains in a consistent state even in the event of a failure or error.

Since all of the above is not that hard to understand, why do so many people claim that the Aggregate pattern is overrated and we should use the word “entity”, or “object”, or something else?

My hypothesis is that it happens because one essential property of the pattern is often missed. Aggregates consolidate entities not only to keep the state consistent or to encapsulate the behaviour within strict boundaries. Aggregates also express new behaviour, which individual entities inside the aggregate don’t have.

A classic example would be an order, which consists of one or more order lines. Individual order lines can be added or removed from the order. You can also change the quantity of the ordered product for each individual line. However, only the whole order can be paid or shipped. Of course, if the order can be partially shipped, but it is usually done by splitting it in two, where one gets shipped, and the other one becomes back-ordered.

In that example, we can clearly see that the Order aggregate expresses the behaviour which individual entities that the aggregate consists of do not have.

This particular characteristic of an Aggregate brings us closer to what Aggregate means in science.

In science, the term Aggregate typically refers to a collection of individual items that are grouped together to form a single, unified whole. This whole can be a physical structure, such as a cluster of cells or particles, or a conceptual construct, such as a group of related ideas or theories.

The term Entity is often used similarly to refer to a distinct and self-contained thing or concept. In science, entities can be physical objects, such as particles or cells, or they can be abstract concepts, such as mathematical equations or theories. The key idea is that an entity is a distinct and identifiable thing that can be studied or considered independently.

The main difference between “entity” and “aggregate” in science is that an entity is a distinct and self-contained thing, while an aggregate is a collection of individual items that are grouped together to form a single, unified whole. An entity can be a physical object, such as a particle or cell, or an abstract concept, such as a mathematical equation or theory. An aggregate, on the other hand, is typically a physical structure or system made up of multiple individual entities that interact with each other in complex ways.

One key difference between entities and aggregates is that entities typically retain their own distinct characteristics, while aggregates often exhibit emergent behaviour that is not present in the individual entities that make up the aggregate. For example, a flock of birds is an aggregate made up of individual birds, but the flock as a whole behaves differently than the individual birds do. Similarly, a crystal is an aggregate made up of individual atoms or molecules, but the crystal as a whole has different properties than the individual atoms or molecules do.

Keeping that in mind, I conclude that Aggregate is a very good term that describes the exact essence of the pattern, and it’s not at all artificial or confusing.

--

--

Alexey is the Event Sourcing and Domain-Driven Design enthusiast and promoter. He works as a Developer Advocate at Event Store and Chief Architect at ABAX.