Repository pattern
An object that looks like an in-memory collection of aggregates (add, find by ID, remove) and hides how they are stored.
Fowler listed it in Patterns of Enterprise Application Architecture, and Evans made it one of the DDD building blocks: one repository per aggregate root, with methods named in business terms (“find upcoming visits for patient”). Domain code depends on the interface; the database, ORM or external API sits behind it, as a port in hexagonal architecture.
The common failure is the generic repository: one class per table with every query anyone ever needed, which leaks the database into the domain and gives nothing an ORM did not already give. For simple CRUD screens or read-heavy reports, querying directly (a transaction script or a read model) is often the clearer choice.
Sources
- Martin Fowler: Repository martinfowler.com
Related terms
- Aggregate (DDD): A cluster of objects changed as one unit, with a single root that enforces the business rules and marks the boundary of one transaction.
- Entity (DDD): A domain object with its own identity that stays the same while its data changes, such as a patient, an order or a doctor’s profile.
- Hexagonal architecture (ports and adapters): Business logic in the middle defines interfaces (ports); databases, APIs and user interfaces plug in from outside as adapters.
- Transaction script: One procedure per business operation that reads data, applies the rules and writes the result, with no domain model in between.
