Showing posts with label Spring Data. Show all posts
Showing posts with label Spring Data. Show all posts

Saturday, 5 July 2014

Repository Design Pattern

🚀 Master Enterprise Java & Software Architecture!

Subscribe to Ram N Java for clean, visual, and beginner-friendly programming tutorials!

🔔 Subscribe Now

What is the Repository Design Pattern?

The Repository Pattern is an essential Object-Relational Metadata Mapping Pattern described in Martin Fowler's classic book, Patterns of Enterprise Application Architecture (PofEAA).

Core Definition: A Repository mediates between the domain logic and data mapping layers using a collection-like interface for accessing domain objects.

In enterprise systems, the repository acts as an intermediate abstraction between your application’s client business logic and the underlying data source—whether that data source is a relational database, a web service, or an external API.

How the Repository Pattern Works

  • Querying Data: When the business logic needs records, it asks the repository. The repository queries the data source (using JDBC, Hibernate, or HTTP calls), maps the raw data into domain business entities, and hands those entities back to the business layer.
  • Persisting Data: When the business layer wants to create or update records, it sends business entity objects to the repository. The repository extracts their field values and persists them cleanly to the data source.

Real-World Example: Person Table and PersonRepository

Consider a database table named Person with columns: id (Primary Key), name, age, and gender.

In our Java application, we define a matching Person domain entity and encapsulate all data access behind a PersonRepository:

public interface PersonRepository {
    void savePerson(Person person);
    void updatePerson(Person person);
    void deletePerson(Long personId);
    Person findPerson(Long personId);
    List<Person> getPersons();
}

When the business service wants to fetch details for a person like "Joe" (ID = 1), it simply executes:

Person joe = personRepository.findPerson(1L);

The client logic never knows whether the data came from MySQL, PostgreSQL, Hibernate, or an external REST endpoint.

Key Benefits of the Repository Pattern

  • Decouples Business Logic: Keeps core business rules completely independent of database queries, drivers, and API protocols.
  • Centralizes Data Access: Eliminates duplicate SQL queries and provides a single maintenance point for database interactions.
  • Simplifies Unit Testing: Provides a natural substitution point where repositories can easily be mocked or stubbed during JUnit tests without connecting to a live database.
  • Foundation of Spring Data: Powers modern frameworks like Spring Data JPA (e.g., JpaRepository and CrudRepository).

Recommended Design Pattern Tutorials:


Click here to watch in Youtube : https://www.youtube.com/watch?v=mb6bwnEaZ3U

Click the below Image to Enlarge
Repository Design Pattern
Repository Design Pattern
See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Tutorials