Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to use the Repository pattern when working in ASP.NET MVC with ORM solutions?

I am bit curious as to what experience other developers have of applying the Repository pattern when programming in ASP.NET MVC with Entity Framework or NHibernate. It seems to me that this pattern is already implemented in the ORM themselves. DbContext and DbSet<T> in the Entity Framework and by the ISession in NHibernate. Most of the concerns mentioned in the Repository pattern - as catalogued in POEE and DDD - are pretty adequately implemented by these ORMs. Namely these concerns are,

  • Persistence
  • OO View of the data
  • Data Access Logic Abstraction
  • Query Access Logic

In addition, most of the implemententations of the repository pattern that I have seen follow this implementation pattern - assuming that we are developing a blog application.

NHibernate implementation:

public class PostRepository : IPostRepository {     private ISession _session;      public PostRepository(ISession session)     {         _session = session;     }      public void Add(Post post)     {         _session.Save(post);     }      // other crud methods.  } 

Entity Framework:

public class PostRepository : IPostRepository {     private DbContext _session;      public PostRepository(DbContext session)     {         _session = session;     }      public void Add(Post post)     {         _session.Posts.Add(post);         -session.SaveChanges();     }      // other crud methods.  } 

It seems to me that when we are using ORMs - such as Nhibernate or Entity Framework - creating these repository implementation are redundant. Furthermore since these pattern implementations does no more than what is already there in the ORMS, these act more as noise than helpful OO abstractions. It seems using the repository pattern in the situation mentioned above is nothing more than developer self aggrandizement and more pomp and ceremony without any realizable techical benefits. What are your thoughts ??

like image 369
Bikal Lem Avatar asked Jan 18 '11 13:01

Bikal Lem


People also ask

Is ORM a repository pattern?

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.

Is repository pattern needed?

No, the repository/unit-of-work pattern (shortened to Rep/UoW) isn't useful with EF Core. EF Core already implements a Rep/UoW pattern, so layering another Rep/UoW pattern on top of EF Core isn't helpful.

Why do 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 do we need repository patterns in C#?

Repository pattern is one of the preferred patterns to apply in an application because it allows programmers to integrate all of the fundamental data operations related to an entity in one main class. Without this pattern, developers would need to create multiple classes for each entity with the same logic.


1 Answers

The answer is no if you do not need to be able to switch ORM or be able to test any class that has a dependency to your ORM/database.

If you want to be able to switch ORM or be able to easily test your classes which uses the database layer: Yes you need a repository (with an interface specification).

You can also switch to a memory repository (which I do in my unit tests), a XML file or whatever if you use repository pattern.

Update

The problem with most repository pattern implementations which you can find by Googling is that they don't work very well in production. They lack options to limit the result (paging) and ordering the result which is kind of amazing.

Repository pattern comes to it's glory when it's combined with a UnitOfWork implementation and has support for the Specification pattern.

If you find one having all of that, let me know :) (I do have my own, exception for a well working specification part)

Update 2

Repository is so much more than just accessing the database in a abstracted way such as can be done by ORM's. A normal Repository implementation should handle all aggregate entities (for instance Order and OrderLine). Bu handling them in the same repository class you can always make sure that those are built correctly.

But hey you say: That's done automatically for me by the ORM. Well, yes and no. If you create a website, you most likely want to edit only one order line. Do you fetch the complete order, loop through it to find the order, and then add it to the view?

By doing so you introduce logic to your controller that do not belong there. How do you do it when a webservice want's the same thing? Duplicate your code?

By using a ORM it's quite easy to fetch any entity from anywhere myOrm.Fetch<User>(user => user.Id == 1) modify it and then save it. This can be quite handy, but also add code smells since you duplicate code and have no control over how the objects are created, if they got a valid state or correct associations.

The next thing that comes to mind is that you might want to be able to subscribe on events like Created, Updated and Deleted in a centralized way. That's easy if you have a repository.

For me an ORM provides a way to map classes to tables and nothing more. I still like to wrap them in repositories to have control over them and get a single point of modification.

like image 117
jgauffin Avatar answered Oct 08 '22 01:10

jgauffin