Skip to main content

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:

  1. Encapsulate the logic for loading and saving domain objects.
  2. Let the domain work with models without knowing about SQL, an ORM, or files.
  3. 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.