liveBook · Manning

Repository pattern

As explained in Simple Object-Oriented Design: Create clean, maintainable applications

A repository is a component that encapsulates the logic for retrieving and persisting domain information.

Use a repository to encapsulate the logic for retrieving and persisting domain information. Keep that responsibility behind a domain-facing interface so database and library details stay in infrastructure rather than spreading through the system. section 1.4 section 6.1.4

This separation controls complexity over time: callers depend on behaviors such as finding and saving employees, while one implementation handles the persistence technology. Changing database access or adding caching can then remain localized. section 6.1.4

Employee persistence with a replaceable implementation

Define the required behavior without exposing its storage mechanism: interface EmployeeRepository { Employee findById(int id); Set<Employee> findByLastName(String lastName); void save(Employee employee); } Implement it in infrastructure, where Hibernate, Postgres, and caching can be handled. A HibernateEmployeeRepository can check a cache before querying the database, without changing the rest of the system. section 6.1.4

Treat the repository as a practical boundary, not an excuse for unnecessary elegance. Its value is clearest when infrastructure changes independently; interface design still requires care, and caching can introduce complications beyond this small example. section 6.1.4

How Simple Object-Oriented Design: Create clean, maintainable applications covers it

How deep it goes
Defines repository responsibilities and demonstrates interface replacement, but defers the detailed challenges of implementing caching.
Examples
Uses EmployeeRepository, HibernateEmployeeRepository, findById, findByLastName, save, Hibernate, Postgres, and a cache.
What it assumes
Assumes object-oriented domain models and distinguishes entities, services, repositories, and infrastructure.
What you can do after
Apply it via a repository interface that allows persistence implementations and infrastructure optimizations to change independently.
How it connects
Connects entities, services, domain models, infrastructure, layered architecture, Hibernate, Postgres, and caching.

Also explained in

Compare how 9 books explain repository pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Simple Object-Oriented Design: Create clean, maintainable applications. The section links point to the parts of the book it draws on.