ASP.NET MVC 3 最佳实践/设计

2024-01-18

我花了很多时间寻找使用 EF 4.1 或其他 ORM 设计 ASP.NET MVC 3 网站的最佳实践。我找到了这个在 ASP.NET MVC 应用程序中实现存储库和工作单元模式的教程 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application。这是一个很好的教程,我学到了一些东西。所以这让我思考并想从堆栈上的人那里知道这是你会使用的东西吗?如果不是,您为什么以及如何设计您的网站?我只是想学习正确的做事方法,并了解为什么一种方法比另一种方法更好。


首先要说实体框架实现了存储库模式和工作单元模式。在像实体框架这样的现代 ORM 之上实现存储库和工作单元模式是一个额外的抽象层:

  • 缩小了底层 ORM 的可能性
  • 不为底层 ORM 提供附加价值
  • 充其量是毫无价值但可能有害

此类实现的目的之一是将查询逻辑封装在存储库中,而不是像单一职责原则(SRP http://en.wikipedia.org/wiki/Single_responsibility_principle)会建议,但有几个查询,因此违反了建议零售价。您可以做的就是依靠 ORM 并将广泛的查询逻辑封装在单个 Query 类中。

我的建议不是通过在抽象层上添加抽象层等来取悦“最佳实践”,而是尝试使用一些更通用的设计指南来解决问题,例如SOLID http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29.

Ayende Rahien 在一系列博客文章中评论了 Northwind Starter Kit 应用程序 (here http://ayende.com/blog/153059/northwind-starter-kit-review-if-you-wonrsquo-t-respect-the-database-there-will-be-pain, here http://ayende.com/blog/153699/ask-ayende-repository-for-abstracting-multiple-data-sources, here http://ayende.com/blog/153153/northwind-starter-kit-review-refactoring-to-an-actual-read-model, here http://ayende.com/blog/153058/northwind-starter-kit-review-data-access-review-thoughts, here http://ayende.com/blog/153057/northwind-starter-kit-review-the-parents-have-eaten-sour-grapes-and-the-childrenrsquo-s-teeth-are-set-on-edge, here http://ayende.com/blog/153029/northwind-starter-kit-review-data-access-and-the-essence-of-needless-work-part-ii, here http://ayende.com/blog/153028/northwind-starter-kit-review-data-access-and-the-essence-of-needless-work-part-i and here http://ayende.com/blog/152706/application-review-northwind-starter-kit) in his blog http://ayende.com/blog涉及应用程序中应用的所谓最佳实践。这是一本很棒的书!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.NET MVC 3 最佳实践/设计 的相关文章

随机推荐