liveBook · Manning

Repository pattern

As explained in Data-Oriented Programming in Java

A repository is an abstraction that provides collection-like access to domain objects while hiding the underlying data storage mechanism.

A repository gives application code collection-like access to domain data while hiding how that data is stored. The abstraction is useful, but its shape should follow the domain and the storage system rather than an imagined future. section 5.1.3 section 10.4.7

Repository designs commonly expose these tensions:

  • ORM-oriented designs often generate one repository or DAO per entity, with standard CRUD operations delegated to a service layer. section 5.1.3
  • A repository can become an oversized file loader whose methods grow whenever a new data type appears; simple replacement may be preferable to speculative extensibility. section 10.4.5
  • The repository understands storage constraints that application code may miss, such as fast primary-key retrieval versus slow full scans. section 10.4.7

A repository at the application boundary

class CustomerRepo { private DatabaseLib db; public Customer getById(String customerId) {...} public void save(Customer customer) {...} } A service can coordinate CustomerRepo, InvoiceRepo, RulesRepo, and FeesRepo, while each repository hides its particular data-access mechanism. If the database exposes get(primaryKey) and scan(expression), the repository can prevent callers from casually choosing an expensive scan. section 5.1.3 section 10.4.7

Table 10.2 Methods exposed for retrieving data
Method
Description
TableRecord get(
    String primaryKey
)
Retrieves an item by its primary key. Fast.
List<TableRecord> scan(
    PredicateExpr expression
)
This is a non-indexed crawl over every item in the database. Flexible, but slow.

Compare primary-key retrieval with non-indexed scanning to see why repository methods should reflect storage costs. section 10.4.7

How Data-Oriented Programming in Java covers it

How deep it goes
Contrasts generated CRUD repositories with evolving file loaders and explains how storage performance should shape exposed methods.
Examples
Uses CustomerRepo, entity-specific CRUD repositories, FileRepository loaders, and get-versus-scan database operations.
What it assumes
Assumes intermediate Java and object-oriented application design, while illustrating repositories through entities, services, databases, and file ingestion.
What you can do after
Evaluate repository boundaries, implement focused data-access methods, and diagnose abstractions that obscure storage costs or accumulate needless indirection.
How it connects
Connects repositories to data access objects, object-relational mapping, service layers, databases, and application-tier orchestration.

Also explained in

Compare how 11 books explain repository pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.