advanced
Aggregates
Group entities and value objects behind a consistency boundary that controls invariant updates and transaction scope.
Aggregates group entities and value objects behind one consistency boundary with a single aggregate root as the only external mutation entry. They define transaction scope—invariants that must hold atomically—and prevent tangled references across boundaries. Cross-aggregate updates use IDs and eventual coordination.
On interviews: choose aggregate roots for an e-commerce cart or invoice. Explain small aggregates versus large ones and invariant enforcement.
Common pitfalls: aggregate roots that span entire object graphs; direct mutation of child entities from outside; distributed transactions trying to lock multiple aggregates.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- One root controls internal consistency.
- External references use identifiers only.
- Size aggregates for real transaction needs.
- Domain events signal cross-aggregate reactions.