Topic 055 / Advanced
Entities, value objects and aggregates
Chapter 4.5 · Domain-Driven Design
What and why
Once a ubiquitous language exists (→ 54), the code still needs concrete building blocks that distinguish "this thing has an identity that persists through change" from "this thing is fully defined by its data" from "this group of things must change together or not at all" — collapsing all three into generic records loses exactly the information that protects correctness. An entity has identity that persists across changes to its attributes (Order #123 is still Order #123 after its status changes). A value object has no identity of its own and is defined entirely by its attributes, so it is naturally immutable, and two value objects with equal attributes are interchangeable (Money, an Address). An aggregate is a cluster of entities and value objects with one designated root, treated as one consistency boundary — a transaction never spans more than one aggregate. Builds on: → 54.
Diagram
Order aggregate (root: Order)
┌──────────────────────────────────┐
│ Order (entity — has identity, │
│ id=123, persists across edits) │
│ ├── ShippingAddress (value │
│ │ object — immutable, equal │
│ │ if same attributes) │
│ └── LineItem[] (entities, exist │
│ only inside this Order) │
└──────────────────────────────────┘
A transaction may update Order and its LineItems together (one aggregate).
It must never, in one transaction, also update a different Order's aggregate.
How it works
- Ask of every concept: "does it need to be the same one over time, even after every field changes?" — yes means entity; give it an id.
- Ask: "is this fully described by its current values, with no history or identity to track?" — yes means value object; make it immutable and compare by value, not reference.
- Group entities and value objects that must stay consistent together under one aggregate root; the root is the only object the outside world may reference or load directly.
- Enforce invariants inside the aggregate's own methods, not from outside — an Order
aggregate rejects adding a line item to a cancelled order from within
Order.addLineItem(), not from a service that might forget to check. - Keep aggregates small: one transaction per aggregate per write. If an operation seems to need two aggregates updated atomically, that is a signal for eventual consistency and a saga (→ 139), not a bigger aggregate.
Practical example
(illustrative). For an e-commerce domain: Order is the
aggregate root; ShippingAddress and Money are value objects, compared by value;
LineItem entities exist only inside an Order's lifecycle and are loaded through the
Order, never fetched independently by a repository that bypasses the Order's invariants.
When to use it
- Use when: invariants span more than one field or entity and need one place to be enforced and one transaction to stay consistent.
- Avoid when: the data is simple reference data — a
TagorCategoryrarely needs aggregate ceremony. - Failure scenarios: an aggregate drawn too large (the whole customer account, including every order and line item, as one aggregate) — every edit to any order now locks or reloads the entire account, and write contention appears where there was no real consistency requirement.
- Simpler alternative: for data with no real invariants to protect, ordinary entities/records with no aggregate boundary are enough.
Practice
(15–20 min; no lab services — any format; includes modeling the Order aggregate from this chapter's exercise). Classification drill: label eight items from an e-commerce domain (Order, ShippingAddress, LineItem, Customer, OrderId, Money, ProductSku, OrderStatus) as entity or value object, justifying by the identity test. Then model the Order aggregate: root, entities, value objects, and one invariant enforced inside it. Done when you can justify the aggregate's boundary by naming the invariant that would break if it were drawn larger or smaller.
Key takeaway
Entities have identity, value objects don't, aggregates are the transaction boundary around both. Done when you can: justify an aggregate boundary by the invariant it protects. Used next in → 56 (aggregate boundaries are the smallest unit bounded contexts are built from), → 71 (concurrent writes to one aggregate are exactly what optimistic locking protects).
References
None — this topic makes no external claims.
Source directory