Showing posts with label Repository Pattern. Show all posts
Showing posts with label Repository Pattern. 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
  • Saturday, 24 August 2013

    Object-Relational Metadata Mapping Patterns

    🚀 Master Enterprise Java & Software Architecture!

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

    🔔 Subscribe Now

    What are Object-Relational Metadata Mapping Patterns?

    In enterprise application architecture, bridging object-oriented domain models with relational database tables often requires repetitive, manual mapping code.

    In Martin Fowler's classic book, Patterns of Enterprise Application Architecture (PofEAA), Object-Relational Metadata Mapping Patterns describe architectural strategies that eliminate hardcoded persistence routines. By storing details of how domain classes and properties map to tables and columns in metadata (such as XML configurations, annotations, or runtime definitions), applications can automate database interactions dynamically.

    The 3 Patterns Under Object-Relational Metadata Mapping

    There are three major patterns that fall under the Object-Relational Metadata Mapping category:

    1. Metadata Mapping Pattern

    Holds details of object-relational mapping in metadata (like annotations or configuration files). Instead of writing bespoke SQL queries for every domain entity, a generic data mapper reads this metadata at runtime to dynamically formulate SQL queries, load entity fields, and persist records.

    2. Query Object Pattern

    Represents a database query as an object structure rather than raw SQL text. It encapsulates database criteria, joins, and sorting parameters using object-oriented methods, allowing applications to build database-agnostic queries safely (analogous to the Criteria API in JPA/Hibernate).

    3. Repository Pattern

    Mediates between the domain logic and data mapping layers using a collection-like interface for accessing domain objects. It completely abstracts database operations, allowing developers to query and manage business entities as if they were stored in an ordinary in-memory collection.

    Why Are These Patterns Important?

    • Drives Modern ORM Frameworks: Forms the core engine of industry-standard tools like Hibernate, JPA, and Spring Data.
    • Reduces Boilerplate Code: Developers define entity relationships through clean annotations (like @Entity, @Table, @Column) rather than handwriting hundreds of repetitive SQL statements.
    • Promotes Clean Architecture: Keeps domain business rules decoupled from database dialects and schema structures.

    Recommended Design Pattern Tutorials:


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

    Click the below Image to Enlarge
    Object-Relational Metadata Mapping Patterns















    See also:

  • All Design Patterns Links
  • Tutorials