Efcore: Question: Are Repositories need any longer?

Created on 24 Mar 2016  路  8Comments  路  Source: dotnet/efcore

I was doing some research and someone pointed out that Line 24 of the DBContext Class states

DbContext is a combination of the Unit Of Work and Repository patterns.

Does this mean that we no longer need to abstract EF to a Repository and then use and Interface to inject it into Controllers?

Most helpful comment

It's really up to you. There are two main reasons you may want to introduce a repository which are not handled by DbContext:

  • Removing any strong coupling between your app logic and data access layer. Though in reality, even with this abstraction it is very difficult to swap out a data access layer unless you build and test for multiple data access technologies right from the start.
  • Constraining the operations that the app layer can perform on the database. DbContext exposes IQueryable so the app can compose any query allowed by LINQ. Repositories that don't return IQueryable can help you control what is sent to the database.

Ultimately it comes down to a philosophical/style/preference decision. Many apps have been built successfully using either approach.

All 8 comments

It's really up to you. There are two main reasons you may want to introduce a repository which are not handled by DbContext:

  • Removing any strong coupling between your app logic and data access layer. Though in reality, even with this abstraction it is very difficult to swap out a data access layer unless you build and test for multiple data access technologies right from the start.
  • Constraining the operations that the app layer can perform on the database. DbContext exposes IQueryable so the app can compose any query allowed by LINQ. Repositories that don't return IQueryable can help you control what is sent to the database.

Ultimately it comes down to a philosophical/style/preference decision. Many apps have been built successfully using either approach.

@rowanmiller Thanks for the insights. I currently working on a project in which i find my repositories have a lot of methods like FindById, FindByName, FindBy.... and also a lot of dynamicness needed for When to Include and When to OrderBy etc..

I just started research

No, repository pattern for EF context is evil! Just don't use it!

@ilmax I personally prefer using repositories in my data access layer for encapsulating DbContext, so other developers can't use it directly in the application and create repetitive queries. I'd like to hear your point of view why not to use this approach.

FYI, Programmers Stack Exchange is a good place to discuss these types of design questions.

@ge6a93 this is not the best place to discuss design, anyway there are a tons of good posts int the wild on this same subject :)

Closing this one out as I think we have addressed everything from the EF side.

I posted this on Stack Exchange as suggested. I would appreciate the discussion continueing there

http://programmers.stackexchange.com/questions/314266/are-repositories-needed-any-longer-in-asp-net-5-ef7

Was this page helpful?
0 / 5 - 0 ratings