Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Repository Pattern or please explain it to me?

I am learning repository pattern and was reading Repository Pattern with Entity Framework 4.1 and Code First and Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle about how they implement the repository pattern with Entity Framework.

Saying

•Hide EF from upper layer
•Make code better testable

Make code better testable I do understand, but why hide EF from upper layer?

Looking at their implementation, it seems just wrap the entity framework with a generic method for query the entity framework. Actually what's the reason for doing this?

I am assuming is for

  1. Loose coupling (that's why hide EF from upper layer?)
  2. Avoid repeat writting same LINQ statement for same query

Am I understand this correctly?

If I write a DataAccessLayer which is a class have methods

QueryFooObject(int id) { ..//query foo from entity framework }   AddFooObject(Foo obj) { .. //add foo to entity framework } ...... QueryBarObject(int id) { .. }  AddBarObject(Bar obj) { ... } 

Is that also a Repository Pattern?

Explaination for dummy will be great :)

like image 869
King Chan Avatar asked Jan 05 '12 20:01

King Chan


People also ask

Why do we use repository pattern?

The Repository pattern allows you to easily test your application with unit tests. Remember that unit tests only test your code, not infrastructure, so the repository abstractions make it easier to achieve that goal.

Why we use repository pattern in MVC?

The repository pattern is intended to create an abstraction layer between the data access layer and the business logic layer of an application. It is a data access pattern that prompts a more loosely coupled approach to data access.

Why we use repository pattern in Web API?

What is a Repository pattern and why should we use it? With the Repository pattern, we create an abstraction layer between the data access and the business logic layer of an application. By using it, we are promoting a more loosely coupled approach to access our data from the database.

Why we need the repository pattern in C#?

The main advantage to use the repository design pattern is to isolate the data access logic and business logic. So that if we do any changes in any of this logic, then that should affect other logic. So let us discuss the step by step procedure to implement the repository pattern in C#.


1 Answers

I don't think you should.

The Entity Framework is already an abstraction layer over your database. The context uses the unit of work pattern and each DBSet is a repository. Adding a Repository pattern on top of this distances you from the features of your ORM.

I talked about this in my blog post: http://www.nogginbox.co.uk/blog/do-we-need-the-repository-pattern

The main reason adding your own repository implementation is so that you can use dependency injection and make your code more testable.

EF is not very testable out of the box, but it's quite easy to make a mockable version of the EF data context with an interface that can be injected.

I talked about that here: http://www.nogginbox.co.uk/blog/mocking-entity-framework-data-context

If we don't need the repository pattern to make EF testable then I don't think we need it at all.

like image 194
Richard Garside Avatar answered Sep 22 '22 15:09

Richard Garside