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.

  3. 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. 
  4. Related 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
  5. Specific Considerations
    1. MSDN:Using Repository and Unit of Work patterns with Entity Framework 4.0
    2. CodeProject:Repository Pattern with Entity Framework 4.1 and Code First
    3. MSDN:The Repository Pattern
    4. geekswithblogs:Creating a Generic Entity Framework 4.0 Repository