1

Is it a best practice to implement repository pattern with entity framework 6 (and upper versions)? why? It seems that Microsoft doesn't recommend it!

Hadelo
  • 53
  • 7
  • I'm also looking for an answer to this question. I was going to study the newest ContosoUniversity tutorial with Entity Framework, but I think it will be better to start from older one which uses repositories, because I'm still learning and my books are based on repository, it would be too much confusion for me. I think most programmers are still using repositories, even if Microsoft doesn't recomend it any longer, so continuing with them shouldn't be a problem at all. – alcohol is evil Aug 06 '16 at 07:44

1 Answers1

1

I think it is a good idea to add repository pattern over entity-framework as it can help you alot in many areas. But it can also add a new layer of complexity. So points to consider are:

  • Using repository you can limit clients to specific operations. (Can be a pro or con depending on requirements and implementation)
  • You can also provide ready made functions for complex operations so client don't have to repeats that logic.
  • Repositories can be made thread safe as DbContext isn't.
  • Repositories will allow you to be independent of entity framework so in future if you ever need to move away from it you can just changed underlying functionality easily.
  • You can intercept incoming db operations in repository and do whatever you like with them. e.g. add addition where clause in multi-company scenario.
  • Testing becomes more easier as it becomes easy to mock underlying functionality.

But Repositories also have others cons. Look at these Is the Repository pattern useful with Entity Framework? and Benefit of Unit of Work and Repository Pattern with Entity Framework

Community
  • 1
  • 1
Shoaib Shakeel
  • 1,447
  • 18
  • 24
  • According to this [post](http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/advanced-entity-framework-scenarios-for-an-mvc-web-application#sql) its not neccessary to implement repository (some how i think it's not recomended too, at least for small and middle size projects) – Hadelo Apr 21 '15 at 10:56