What problem does the Repository pattern solve?
Options
- Version control of the source code of a software project
- Hiding persistence behind a collection-like interface (Correct option)
- Caching HTTP responses to reduce the load on the server
- Managing package dependencies between the modules
Answer
Hiding persistence details behind a collection-like interface so domain logic does not depend on the ORM/SQL
Why
A repository is a class like UserRepository with simple methods: get(id), add(user), list_active(). All SQL or ORM code lives inside it.
The business logic only talks to the repository, so it does not care whether data comes from PostgreSQL, an API or a dictionary in memory.
In tests, you swap in an in-memory repository: fast and no database needed.
Remember
Repository = a clean door between business logic and the database.