Aggregates, Repositories and Factories
Protect invariants with aggregates and persist them cleanly.
Protecting invariants
Once you have entities and value objects, you need patterns to keep clusters of them consistent and to persist them cleanly. DDD answers with three tactical patterns: the Aggregate (a consistency boundary), the Repository (a collection-like persistence abstraction), and the Factory (complex construction). This lesson shows how they fit together in PHP.
What is an Aggregate
An Aggregate is a cluster of entities and value objects treated as a single unit for data changes. One entity is the Aggregate Root — the only member outside code may hold a reference to. All modifications go through the root, which enforces the aggregate's invariants. The aggregate is also the transactional boundary: it is loaded and saved atomically.
All lessons in this course
- DDD Building Blocks: Entities and Value Objects
- Aggregates, Repositories and Factories
- Domain Events and Domain Services
- Bounded Contexts and Context Mapping