编写MVC应用程序的最佳实践是什么?

时间:2021-08-02 16:20:21

Coming from ASP.NET Webform and Classic ASP. I want to know what are the best practices. Are MVC apps would not really be using Server Controls and ViewStates? If so the persisting of the html form controls would be similar to classic ASP. Thanks

来自ASP.NET Webform和Classic ASP。我想知道什么是最佳实践。 MVC应用程序是不是真的会使用Server Controls和ViewStates?如果是这样,html表单控件的持久性将类似于传统的ASP。谢谢

1 个解决方案

#1


Some of the big ones I learned in my move:

我在移动过程中学到的一些重要内容:

1) Views should have very limited logic. If you find you have lots of <% if(...) { %> statements, you need to add more information to your models and/or create HtmlHelper extension methods that handle that logic.

1)观点应该具有非常有限的逻辑。如果您发现有很多<%if(...){%>语句,则需要向模型添加更多信息和/或创建处理该逻辑的HtmlHelper扩展方法。

2) Your controller classes should only interpret user input and request data from your models. If decisions have to be made about the data, the controller should push the information required to make those decisions to the models.

2)您的控制器类应仅解释用户输入并从模型中请求数据。如果必须决定数据,控制器应将做出这些决策所需的信息推送到模型中。

3) The models should keep all your business logic relating to data. Partial classes are your friends here. Lots of DAL generators let you use partials to add logic to your generated models (such as Linq to Sql and Subsonic).

3)模型应保留与数据相关的所有业务逻辑。部分课程是你的朋友。许多DAL生成器允许您使用partials为生成的模型添加逻辑(例如Linq to Sql和Subsonic)。

4) State is the enemy. That's not just MVC; I just like pointing out that the more state your design requires, the easier it breaks.

4)国家是敌人。那不仅仅是MVC;我只是想指出你的设计需要的状态越多,它就越容易破碎。

#1


Some of the big ones I learned in my move:

我在移动过程中学到的一些重要内容:

1) Views should have very limited logic. If you find you have lots of <% if(...) { %> statements, you need to add more information to your models and/or create HtmlHelper extension methods that handle that logic.

1)观点应该具有非常有限的逻辑。如果您发现有很多<%if(...){%>语句,则需要向模型添加更多信息和/或创建处理该逻辑的HtmlHelper扩展方法。

2) Your controller classes should only interpret user input and request data from your models. If decisions have to be made about the data, the controller should push the information required to make those decisions to the models.

2)您的控制器类应仅解释用户输入并从模型中请求数据。如果必须决定数据,控制器应将做出这些决策所需的信息推送到模型中。

3) The models should keep all your business logic relating to data. Partial classes are your friends here. Lots of DAL generators let you use partials to add logic to your generated models (such as Linq to Sql and Subsonic).

3)模型应保留与数据相关的所有业务逻辑。部分课程是你的朋友。许多DAL生成器允许您使用partials为生成的模型添加逻辑(例如Linq to Sql和Subsonic)。

4) State is the enemy. That's not just MVC; I just like pointing out that the more state your design requires, the easier it breaks.

4)国家是敌人。那不仅仅是MVC;我只是想指出你的设计需要的状态越多,它就越容易破碎。