Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a reason for using the Repository pattern with Entity Framework if I know I will only ever use EF?

From reading various books and articles, I seem to rather often find the usage of the Repository-pattern suggested. I get the point if you need to be able to swap out your data layer from one to another, but my question is, if I know with 100% certainty that I will not use any other tech for data access, is there any reason for using said pattern?

The thing that I find myself doubting the most is that I don't really see what this extra layer of abstraction can bring to the table in this scenario. From my experience, EF with its fluent linq-to-entities -functionality should be more than enough for pretty much all my needs.

The most usual cases seem to start the repositories with methods such as FindAll, Find, Add and Delete, all of which are very easily accessible directly through EF (so no code duplication to speak of).

So am I just missing some big point, or is the repository more for when you need to support multiple different data access technologies?

like image 751
bobblez Avatar asked Mar 31 '13 21:03

bobblez


People also ask

Do we need repository pattern with Entity Framework?

The single best reason to not use the repository pattern with Entity Framework? Entity Framework already implements a repository pattern. DbContext is your UoW (Unit of Work) and each DbSet is the repository. Implementing another layer on top of this is not only redundant, but makes maintenance harder.

What is the benefit of repository pattern in Entity Framework?

The Repository Pattern allows us to create an abstraction layer between the data access layer and the business logic layer of an application. So, this Data Access Pattern offers a more loosely coupled approach to data access.

Why should we use repository pattern?

The Repository pattern makes it easier to test your application logic. 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 is the repository pattern bad?

Repository Pattern Helps Hide SQL Complexity Now when using ORM the complexity of querying the database is hidden behind these kind of frameworks. So unless you're using SQL directly and you want in memory representation of your database objects, using repository doesn't make any sense.


2 Answers

They are many opinions on the issue but after using repositories for 2 projects, i never tried it again.

Too much pain with hundreds of methods for all those cases with no clear benefits (i'm almost never going to swap out EF for another ORM).

The best advice would be to try it out so you can make an informed opinion on which route to take.

Some opinions against it here

like image 199
scartag Avatar answered Oct 15 '22 21:10

scartag


I think you're on the right direction. I asked myself the same question two years ago after I've used the repository pattern in some projects. I came to the conclusion that hiding your ORM behind a repository implemented on top of your ORM will get you nothing but unnecessary work. In addition to implementing meaningless FindAll, Find, Add ... methods you would loose some performance optimization possibilities that the ORM gives you. Or at least it will get quite hard to apply some of those methods.

So if you're not going to switch your ORM within the lifetime of your project, I don't see any benefits in applying the repository pattern.

So instead of preparing for the situation where one could in future easily switch the ORM, I would suggest to do some more investigation upfront, wisely choose an ORM, stick with it and stay away from the repository pattern.

like image 35
Andre Kraemer Avatar answered Oct 15 '22 21:10

Andre Kraemer