Repositorio
Escrito por Darjus Kazakevic · revisado el
A collection of aggregates that hides the database behind it.
A repository sits between the domain and the database. To the domain it looks like a collection in memory: you add an aggregate, get one by id, or remove one. Tables, joins and connection pools stay hidden behind that interface.
The interface (a port) lives in the domain. The SQL implementation (an adapter) lives in infrastructure. Tests can then use a simple in-memory version, and the database becomes a detail you can change. The cost is one more layer, which is wasted on simple CRUD.
En esencia — Give the domain a collection-like interface for aggregates, so it can store and load them without knowing about SQL, ORMs or the database.
Cuándo recurrir a él
Recurre a él cuando
- You have a real domain model and want it free of storage code
- You want fast use-case tests with an in-memory fake
- Persistence is a plugged-in adapter in a Hexagonal or Clean design
Reconsidéralo cuando
- Simple CRUD, where an ORM or Active Record already does the job
- Reports and list screens: query a read model directly
- A repository that only forwards every call to the ORM
Anatomy of a repository
- Collection-like interface
- The repository reads like a set: add, get by id, remove, and a few named queries. Callers never write insert or update, and never see SQL.
- One per aggregate root
- There is one repository for each aggregate root, never one for the entities inside it. It loads and saves the whole aggregate, so the aggregate boundary stays real.
- Port in the domain, adapter outside
- The interface lives with the domain that uses it. The implementation (SQL, an ORM, an HTTP API) lives in infrastructure and is wired in at startup.
- Two styles of saving
- Collection style tracks changes for you and commits once, usually with a Unit of Work. Persistence style has an explicit save(order). Pick one style per codebase.
Reglas de diseño
- Put the interface in the domain
- The domain owns the port and names it in business words. The SQL adapter implements it from outside. If the interface mentions tables, rows or query builders, the database has leaked back in.
- One repository per aggregate root
- Load and save the whole aggregate through its root. A repository for an inner entity lets callers change that entity without the root checking its rules.
- Return domain objects, not ORM rows
- Map rows to aggregates inside the adapter. A method that returns an ORM entity ties every caller to the ORM, and a schema change then breaks domain code.
- Send reports to a read model
- Repositories serve writes and the reads a use-case needs. List pages and reports with joins and paging should query the database directly, or use a read model.
Errores habituales
- A generic IRepository<T> that exposes a query builder. The domain is coupled to the database again.
- One repository per table instead of per aggregate root, so inner entities change without their root.
- Returning ORM entities instead of aggregates, so a renamed column breaks domain code.
- A repository with dozens of findByXAndY methods. Use a Specification or a read model instead.