intermediate
Repository
Encapsulate persistence queries behind collection-like methods when it clarifies domain access and avoids leaking storage details.
Repository encapsulates persistence behind collection-like methods—`findById`, `save`, `delete`—so application code speaks domain language instead of SQL or ORM details. It clarifies where querying lives and enables in-memory fakes in tests. Repositories should not absorb unrelated business orchestration.
On interviews: contrast Repository with Active Record and raw DAOs. Explain aggregate-root repositories versus generic CRUD for every table.
Common pitfalls: repositories returning ORM entities everywhere; N+1 queries hidden inside convenience methods; god repositories with twenty query variants.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Interface defined in application or domain layer.
- Implementation lives in infrastructure.
- Queries express domain access needs.
- Tests use in-memory or fake repositories.