What is a repository?
A Repository is an interface for accessing domain aggregates or entities that hides the details of data storage.
It behaves like an "in-memory collection," but actually talks to a database, an API, and so on.
The repository's jobs:
- Encapsulate the logic for loading and saving domain objects.
- Let the domain work with models without knowing about SQL, an ORM, or files.
- Support the Dependency Inversion principle: the domain depends on the interface, and infrastructure implements it.
Example:
python
class OrderRepository:
def get_by_id(self, id): ...
def save(self, order): ...The point: a repository is a bridge between the "clean" domain and the technical world of data storage.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.