ASP的最佳实践。NET MVC

时间:2021-03-03 04:06:34

Basically, this 'question' is intended to pool information about best practices in the community. The reasons for this are:

基本上,这个“问题”旨在收集社区中关于最佳实践的信息。这样做的原因是:

  • The distinct lack of an community based ASP.NET MVC resource for best practices. Yes, I am aware of wiki.asp.net.
  • 明显缺乏基于社区的ASP。最佳实践的netmvc资源。是的,我知道wiki.net。
  • I'm painfully aware that the ASP.NET wiki on ASP.NET MVC is woefully small and terribly out of date and needs fleshing out more by Microsoft before the community does anything with it.
  • 我痛苦地意识到ASP。净wiki在ASP。NET MVC非常小,非常过时,在社区使用它之前,需要微软做更多的充实。
  • I'm very much interested in best practices in general, having had a fair degree of experience with ASP.NET MVC over the last year, but I'm also mindful that I'm no expert, and that I can definitely improve. This post, I believe, can help that, and you.
  • 我对总体上的最佳实践非常感兴趣,对ASP有一定的经验。NET MVC在过去的一年里,但是我也注意到我不是专家,我肯定可以改进。我相信这篇文章能帮上忙,还有你。

I gave some serious thought on whether or not I should ask this question, even going to the lengths of creating a question on meta about creating this question. :)

我认真地考虑了我是否应该问这个问题,甚至在元数据上创建了一个关于创建这个问题的问题。:)

What I am looking for is for best practice information on all aspects of ASP.NET MVC. Controllers, models, extensions, HTML helpers, route builders, TDD.

我寻找的是关于ASP的所有方面的最佳实践信息。净MVC。控制器,模型,扩展,HTML助手,路径构建器,TDD。

In the past, I have found resources, such as Kazi Manzur Rashid's best practices (part 1 and 2) post quite useful, along with Maarten Balliauw's weigh-in, but these have dried up somewhat, and I was hoping for a more dynamic and growing repository of information from different people, rather than just the occasional blog entry from learned individuals. Information like this is few and far between, and ASP.NET MVC community resources even less so.

过去,我发现资源,如Kazi Manzur拉希德的最佳实践(1和2)部分文章非常有用,连同Maarten Balliauw称重,但是这些已经枯竭,我希望更有活力和不断增长的存储库的信息从不同的人,而不仅仅是偶尔从学会了个人博客条目。像这样的信息是很少的,而且非常多。NET MVC社区资源更是如此。


MVC in general

Model

Feel free to add model links

请随意添加模型链接

Views

Controller

Feel free to add controller links

请随意添加控制器链接


Guidelines:

  • One "best practice" per answer
  • 每个答案都有一个“最佳实践”
  • Take the time to explain why it should be preferred over the alternatives.
  • 花点时间解释为什么它应该比其他选择更受欢迎。
  • Read existing answers first - if you see one you agree with, up-vote it; if you disagree, down-vote and leave a comment explaining why.
  • 先阅读现有的答案——如果你看到一个你同意的答案,就去投票;如果你不同意,投反对票,并留下评论解释原因。
  • Feel free to edit any answer if you can expand on why it is "best"!
  • 如果你能详述为什么它是“最好的”,请随意编辑任何答案。

P.S. Please don't just put "Anything by Phil Haack, Scott Guthrie, Rob Conery or Scott Hanselman"!

附注:请不要只写“任何菲尔·哈克、斯科特·格思里、罗伯·康纳利或斯科特·汉斯曼的作品”!

7 个解决方案

#1


10  

Use T4MVC to eliminate all magic strings and magic anonymous types in your entire project. This will help you in refactoring later in your project, and (apart from having to still ensure your routes are well-defined) all Action calls get their proper parameters. It changes calls like this:

使用T4MVC在整个项目中消除所有的魔法字符串和神奇的匿名类型。这将帮助您在以后的项目中进行重构,并且(除了仍然需要确保您的路由得到良好的定义之外)所有的操作调用都将获得适当的参数。它会像这样改变调用:

<%= Html.ActionLink("Link text", "Products", "Details", new { id = Model.Id }) %>

Into:

成:

<%= Html.ActionLink("Link text", MVC.Products.Details(Model.Id)) %>

#2


7  

Keep any and all logic out of your view. Your controller should determine what gets shown - your view should be dumb.

把你的想法和逻辑都保留下来。你的控制器应该决定显示什么-你的视图应该是哑的。

#3


7  

  1. IoC/DI for Controller factory (so I can inject IRepository, ISomeService in controllers constructor)
  2. 控制器工厂的IoC/DI(因此我可以在控制器构造函数中注入IRepository,异构服务)
  3. never access HttpContext directly, build wrapper, so it can be unit tested
  4. 不要直接访问HttpContext,构建包装,这样就可以进行单元测试。
  5. Validation framework for model binding validations (xVal or FluentValidation). Built-in validation inside MVC 1 is basic
  6. 模型绑定验证的验证框架(xVal或FluentValidation)。MVC 1中的内置验证是基本的
  7. never use "magic strings": for calling controllers/actions from View, for RouteLink, RenderPartial, RenderAction, ...
  8. 永远不要使用“魔弦”:用于从视图调用控制器/动作,用于RouteLink、RenderPartial、RenderAction,…
  9. never use ViewData, build DTO ViewModel classes. Use AutoMapper for mapping data from domain entities to ViewModel DTO objects for View
  10. 不要使用ViewData,构建DTO ViewModel类。使用AutoMapper将数据从域实体映射到ViewModel DTO对象

ViewModel DTO objects:
BaseViewModel abstract class, with properties for rendering page meta data, menus and all other stuff that appears on every page. All other ViewModel classes inherits from BaseViewModel.

ViewModel DTO对象:BaseViewModel抽象类,具有用于呈现页面元数据、菜单和在每个页面上显示的所有其他内容的属性。所有其他ViewModel类都继承自BaseViewModel。

#4


3  

While not a best practices site, the below site is doing great things with asp.net MVC that could probably be considered best practice.

虽然不是最佳实践站点,但是下面的站点使用asp.net MVC做了一些很好的事情,这可能被认为是最佳实践。

http://www.codeplex.com/MVCContrib

http://www.codeplex.com/MVCContrib

#5


3  

There are 2 free chapters available for the book "MVC in Action".

《MVC in Action》一书有两章是免费的。

http://www.manning.com/palermo/

http://www.manning.com/palermo/

Chapter 12 is best practices.

第12章是最佳实践。

#6


3  

Keep your controllers as "thin" as possible. Simply have them validate data coming in from the form, and then calling the model (which does all the real work) to get the data to be returned in your next View.

让你的控制器尽可能“薄”。只需让它们验证来自表单的数据,然后调用模型(这将完成所有实际工作),以便在下一个视图中返回数据。

Having complex controllers kind of breaks the MVC concept.

拥有复杂的控制器会打破MVC的概念。

#7


1  

This is how i handle all DropDowns in my application and i wanna share with you cause i think it's sorta best practice:

这就是我如何处理我的申请的所有下拉,我想和你们分享,因为我认为这是最好的实践:

One EditorTemplate for all DropDownLists in ASP.Net MVC

一个编辑模板,用于ASP中的所有下拉列表。Net MVC

#1


10  

Use T4MVC to eliminate all magic strings and magic anonymous types in your entire project. This will help you in refactoring later in your project, and (apart from having to still ensure your routes are well-defined) all Action calls get their proper parameters. It changes calls like this:

使用T4MVC在整个项目中消除所有的魔法字符串和神奇的匿名类型。这将帮助您在以后的项目中进行重构,并且(除了仍然需要确保您的路由得到良好的定义之外)所有的操作调用都将获得适当的参数。它会像这样改变调用:

<%= Html.ActionLink("Link text", "Products", "Details", new { id = Model.Id }) %>

Into:

成:

<%= Html.ActionLink("Link text", MVC.Products.Details(Model.Id)) %>

#2


7  

Keep any and all logic out of your view. Your controller should determine what gets shown - your view should be dumb.

把你的想法和逻辑都保留下来。你的控制器应该决定显示什么-你的视图应该是哑的。

#3


7  

  1. IoC/DI for Controller factory (so I can inject IRepository, ISomeService in controllers constructor)
  2. 控制器工厂的IoC/DI(因此我可以在控制器构造函数中注入IRepository,异构服务)
  3. never access HttpContext directly, build wrapper, so it can be unit tested
  4. 不要直接访问HttpContext,构建包装,这样就可以进行单元测试。
  5. Validation framework for model binding validations (xVal or FluentValidation). Built-in validation inside MVC 1 is basic
  6. 模型绑定验证的验证框架(xVal或FluentValidation)。MVC 1中的内置验证是基本的
  7. never use "magic strings": for calling controllers/actions from View, for RouteLink, RenderPartial, RenderAction, ...
  8. 永远不要使用“魔弦”:用于从视图调用控制器/动作,用于RouteLink、RenderPartial、RenderAction,…
  9. never use ViewData, build DTO ViewModel classes. Use AutoMapper for mapping data from domain entities to ViewModel DTO objects for View
  10. 不要使用ViewData,构建DTO ViewModel类。使用AutoMapper将数据从域实体映射到ViewModel DTO对象

ViewModel DTO objects:
BaseViewModel abstract class, with properties for rendering page meta data, menus and all other stuff that appears on every page. All other ViewModel classes inherits from BaseViewModel.

ViewModel DTO对象:BaseViewModel抽象类,具有用于呈现页面元数据、菜单和在每个页面上显示的所有其他内容的属性。所有其他ViewModel类都继承自BaseViewModel。

#4


3  

While not a best practices site, the below site is doing great things with asp.net MVC that could probably be considered best practice.

虽然不是最佳实践站点,但是下面的站点使用asp.net MVC做了一些很好的事情,这可能被认为是最佳实践。

http://www.codeplex.com/MVCContrib

http://www.codeplex.com/MVCContrib

#5


3  

There are 2 free chapters available for the book "MVC in Action".

《MVC in Action》一书有两章是免费的。

http://www.manning.com/palermo/

http://www.manning.com/palermo/

Chapter 12 is best practices.

第12章是最佳实践。

#6


3  

Keep your controllers as "thin" as possible. Simply have them validate data coming in from the form, and then calling the model (which does all the real work) to get the data to be returned in your next View.

让你的控制器尽可能“薄”。只需让它们验证来自表单的数据,然后调用模型(这将完成所有实际工作),以便在下一个视图中返回数据。

Having complex controllers kind of breaks the MVC concept.

拥有复杂的控制器会打破MVC的概念。

#7


1  

This is how i handle all DropDowns in my application and i wanna share with you cause i think it's sorta best practice:

这就是我如何处理我的申请的所有下拉,我想和你们分享,因为我认为这是最好的实践:

One EditorTemplate for all DropDownLists in ASP.Net MVC

一个编辑模板,用于ASP中的所有下拉列表。Net MVC