I posted a question on github to the EF Team. I got a reply saying it would be better to ask this question here so I will copy and paste it here as we as a link so other can see the few replies on GitHub.
Question: 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?
Original post on Github: https://github.com/aspnet/EntityFramework/issues/4899
The reason I ask this is I seem to get into a spot where im adding a lot of methods to the repository like GetById, GetByName, GetWithIncludesABC, GetWithIncludes123, etc.. and it seems to be dirtying the repo in my mind
Best Answer
If you're adding methods to a repository like
Then you're better off moving to a Service Layer, and letting the Service Layer use EF directly. EF already has functionality similar to the above methods that you're just endlessly duplicating.
A Service Layer exposes Business Domain methods, and uses CRUD to implement them. For example, you might have a method called
TransferMoney(A, B)
, where A and B are checking accounts. This allows you to speak the language of your business domain, while the Service Layer handles the CRUD for you.The only compelling reason I can think of where you might want to have a separate Repository Layer is so that you can mock that repository layer or substitute a different data source for testing purposes.