Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repository Pattern Standardization of methods

All I am trying to find out the correct definition of the repository pattern.

My original understanding was this (extremely dumbed down)

  • Separate your Business Objects from your Data Objects
  • Standardize access methods in data access layer.

I have really seen 2 different implementation, and there are no formal examples online, the ones i have seen are tucked away in books.

Implementation 1 :

public Interface IRepository<T>{       List<T> GetAll();       void Create(T p);       void Update(T p); }   public interface IProductRepository: IRepository<Product> {       //Extension methods if needed        List<Product> GetProductsByCustomerID(); } 

Implementation 2 :

public interface IProductRepository {       List<Product> GetAllProducts();       void CreateProduct(Product p);       void UpdateProduct(Product p);       List<Product> GetProductsByCustomerID(); } 

Notice the first is generic Get/Update/GetAll, etc, the second is more of what I would define "DAO" like.

Both share an extraction from your data entities. Which I like, but i can do the same with a simple DAO. However the second piece standardize access operations I see value in, if you implement this enterprise wide people would easily know the set of access methods for your repository.

Am I wrong to assume that the standardization of access to data is an integral piece of this pattern ? If both are correct why would one choose to do implementation 2?

Rhino has a good article on implementation 1, and of course MS has a vague definition and an example of implementation 2 is here.

like image 604
Nix Avatar asked Apr 21 '10 15:04

Nix


People also ask

What is repository method?

From Martin Fowler "Patterns of Enterprise Application Architecture", the definition of the Repository Pattern is: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.

What is a repository pattern?

The Repository pattern. Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

What problem does repository pattern solve?

They decouple application and domain design from persistence technology, multiple database strategies, or even multiple data sources.

What type of pattern is repository pattern?

A repository is a specialisation of the Facade pattern which is structural.


2 Answers

I second the Fowler quote cited by oded. I want to point out that he said "collection-like" interface. How you implement the collection like interface is certainly up to you, but neither can nor should you try to hide the fact it represents a remote datasource. It therefore differs significantly from an in-memory collection, which does not need to flush changes to a remote data store. The change tracking mechanism of your ORM or your roll-your-own solution determines how transparent this can be made to the caller. Deletes usually need to be marked explicitly, inserts are discoverable (persistence by reachability) and updates sometimes need to be marked explicitly too. Combine this with the complicated dependencies of your aggregate roots and you'll see that's not very collection like.

There is no such thing as "the cannonical repository implementation".

There is a constant battle going on between the advocators of a generic repository base class and those who prefer implementing each repository on its own. While the generic implementation is appealing in simple scenarios, you will very often find it to be a very leaky abstraction. For example some of your aggregates may only be soft-deleted (cistomizable via virtual method overrides) while others may not support a delete operation at all.

Make sure you understand the implications of each approach before deciding which route to take. Greg Young has a good post on the merits of generic repositories.

http://codebetter.com/blogs/gregyoung/archive/2009/01/16/ddd-the-generic-repository.aspx

like image 183
Johannes Rudolph Avatar answered Oct 02 '22 11:10

Johannes Rudolph


From Martin Fowler "Patterns of Enterprise Application Architecture", the definition of the Repository Pattern is:

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

So, both approaches are correct.

like image 34
Oded Avatar answered Oct 02 '22 11:10

Oded