I have spent a lot of time searching for best practices for designing a ASP.NET MVC 3 web site using EF 4.1 or another ORM. I found this tutorial on Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application . It was a good tutorial and I learned something. So this got me thinking and wanted to know from the people on stack is this something you would use? if not why and how did you design your web site? I just want to learn the correct way to do things and understand why one way is better then the other.
问题:
回答1:
First to say Entity Framework implements a Repository pattern and a Unit of Work Pattern. Implementing Repository and Unit of Work patterns on top of a modern ORM like Entity framework is an additional layer of abstraction that:
- narrows the possibilities of the underlying ORM
- does not provide additional value to the underlying ORM
- is at best worthless but may be harmful
One purpose of such implementations is to encapsulate the query logic in the Repository and not one query like Single Responsibility Principle (SRP) would suggest, but a couple of queries and thus violating SRP. What you could do is to rely on your ORM and encapsulate extensive query logic in single Query classes.
My suggestion is not to please "best practices" by adding abstraction layers on abstraction layers and so on but to try to solve a problem using some more general design guidelines such as SOLID.
Ayende Rahien reviews the Northwind Starter Kit application in a series of blog posts (here, here, here, here, here, here, here and here) in his blog that deal with so called best practices applied in an application. This is a great read!