c.Repository Pattern

  1. Motivation

      1. To Mediates in between the domain and data mapping layers using a collection-like interface for accessing domain objects.

  2. Summary

    1. The repository mediates between the data source layer and the business layers of the application.

    2. It queries the data source for the data, maps the data from the data source to a business entity, and persists changes in the business entity to the data source.

    3. A repository separates the business logic from the interactions with the underlying data source or Web service.

    4. The separation between the data and business tiers has three benefits:

      1. It centralizes the data logic or Web service access logic.

      2. It provides a substitution point for the unit tests.

      3. It provides a flexible architecture that can be adapted as the overall design of the application evolves.

    5. Repository promotes the Specification pattern which encapsulates the query to be performed in a pure object-oriented way. Therefore, all the code for setting up a query object in specific cases can be removed. Clients need never think in SQL and can write code purely in terms of objects.

  1. When to Use

    1. In a large system with many domain object types and many possible queries.

    2. When you need to facilitate unit testing of domain object.

  2. Related Pattern

    1. The Trusted Facade Pattern

      1. Data Mapper: is often use to query data from data source and pushing to business objects.

    2. Unit of Work:

        1. Is a pattern to track changes of business object and help to save changes to database.

        2. Framework like EF4.0 provide this pattern implemented by default

  1. Specific Considerations