Bookmarks

Aggregate

A cluster of domain objects with a root and a boundary: outside code sees only the root, while the invariants are kept in a consistent state inside the aggregate.

Also: DDD aggregate, aggregate root, aggregate boundary, aggregates in DDD

checked 8 Nov

All terms

An aggregate is a tactical pattern of domain-driven design (DDD): it defines where the transaction boundary lies, what may be changed through the public interface and what must remain consistent at all times. Eric Evans introduced the concept in his 2003 book “Domain-Driven Design”.

The structure of an aggregate

  • The root is an entity with a globally unique identifier; it is the only thing visible from outside.
  • Inner entities and value objects live only inside the boundary and obey the root.
  • References from other aggregates point only to the root, most often by identifier.
  • Invariants that must hold at all times are kept within a single aggregate.

Example: an order

In an online shop the aggregate is an order: the root is Order, and inside are the OrderLine items and the total. The rule “the sum of the items equals the order total” is an invariant, so it is checked inside: outside code does not edit the items directly but calls order.add_item(...).

pythonThe aggregate root itself keeps the invariant “the total is the sum of the items”
            class Order:                def add_item(self, product_id: str, qty: int, price: int) -> None:                    self.lines.append(OrderLine(product_id, qty, price))                    self.total = sum(line.amount for line in self.lines)  # the invariant            

Rules around aggregates

One outside request changes one aggregate; the changes are saved in a single transaction, so the aggregate boundary is also the consistency boundary. When data from a neighbouring aggregate is needed, its root is called or the data is read by identifier; the aggregate reports changes with an event — a domain event. Vernon formulated the rule “one transaction updates one aggregate” in his essay “Effective Aggregate Design”.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • bounded context

    A bounded context is the boundary of the model and its language at the scale of the system, while an aggregate is the consistency boundary inside the model.

  • DDDdomain-driven design

    DDD is the overall approach to designing around a domain; the aggregate is one of its tactical patterns.

In depth in the references

where the topic is covered in detail