📜 ⬆️ ⬇️

Top 12 ASP.NET MVC Practices

image From the translator: here are 12 quite good practices that the author recommends using when developing ASP.NET MVC applications. I decided to translate this short list for those who are just eyeing ASP.NET MVC and taking the first steps in this framework. I hope these tips will help you better understand the architecture of ASP.NET MVC and make the right decisions when developing projects. Some tips are controversial, but do not forget that the author expresses his opinion, which may not coincide with yours.

Best controller practices


1. Delete AccountController
You will never use it and it is a super bad practice to leave a demo code in your applications.

2. Hide controllers from the outside World
Dependencies on HttpContext, data access classes, configuration, logging, hours, etc., make it difficult for your application to test (or cannot be tested at all), to be further developed and modified.

3. Use an IoC container
In order to adhere to rule No. 2, use an IoC container to manage all external dependencies. I use Ninject v2 , but there are a lot of containers and, in addition, you can easily build your own container.
')
4. Say "NO" to the magic lines.
Never use ViewData [“key”], but always create a ViewModel for each View and use strongly typed ViewPage views.

Magic strings are evil because they do not tell you about a grammatical error, if you allow it, because of which your view does not work, instead, using strictly typed models, you can still determine the source of the problem at the compilation stage. And, as a bonus, you get IntelliSense.

5. Build your own agreements.
Use ASP.NET MVC as a base for your (or your company) architecture. Create your own conventions with basic controllers or even views from which your classes will inherit, instead of using the default classes.

6. Pay attention to the types of requests (Verbs)
Even if you are not using a REST model (just a RESTfull), use a specific Http-type request for each action. Accept the PRG (Post-Redirect-Get) pattern : show data with a GET request, modify the data with POST requests.

Best practice models


7. Model domain! = ViewModel
The domain model represents the domain, whereas ViewModel implies the needs of your presentation are met, and these two worlds are (and usually are) different. In addition, the domain model is data plus behavior, it is a hierarchy and is built on the basis of complex types, whereas ViewModel is just DTO (Data Transfer Objects), a flat model based on strings. To avoid the tedious and error-prone mapping code of objects, you can use AutoMapper . Also, I recommend reading a good review: ASP.NET MVC View Model Patterns .

8. Use ActionFilters for shared data.
This is my solution for the best component model of ASP.NET MVC and in the future I should write a few more articles on this topic. You do not need controllers to obtain data that is shared between different views. My solution is to use Action Filter to get the necessary data and share it between views. And use partial views for partial views.

Best Practices for Submissions


9. NEVER use code-behind
NEVER.

10. Write HTML each time you have the opportunity.
I suggest that web developers find it convenient to write HTML (both CSS and JavaScript). Therefore, they should not use HtmlHelpers just to simply hide the HTML (for example, in the form of Html.Submit or Html.Button). And again this topic for future articles.

11. If there is an if, write HtmlHelper
Views should be dumb (dumb) (controllers should be lean and models should be thick). If you suddenly find that you write “if” in a view, then consider creating an HtmlHelper to hide the conditional expression.

12. Pay attention to the choice of your view engine.
By default, ASP.NET MVC uses WebFormViewEngine, but in my opinion, this is not the best choice. I prefer to use the Spark View Engine , as this tool seems to me more suitable for MVC. What I like about it is that the stream is built on HTML and the “foreach” cycles and the “if” expressions are defined via “HTML attributes”.

Downloads


Both slides and demo code are available for download. Or you can watch the slides online .

Progg it

Source: https://habr.com/ru/post/73692/


All Articles