Some of Best Practices in ASP.NET MVC

1. Keep Model as separate class library. 

2. Implement Repository pattern to isolate Business layer from Data Access Layer. 

3. Follow MVC architecture properly 

          a. Controller should contain only user interaction logic

          b. View should contain only UI Design

 

4. Use ViewModel and isolate View from presentation and Data Transformation logic.

 

5. Always have base controller created. Means make sure your controllers are not directly

    inheriting from Controller class instead there should be an intermediate base controller

    Example:

    public class BaseController: Controller{….}

    public class CustomerController:BaseController{}

 

6. For Generating input controls in view always use Html helper class methods.

 

7. Always put appropriate Http verbs on action methods. Example: If it is a post action put 

    HttpPost and if it is Get put HttpGet action. By default it will be both and it should not be. 

 

8. Strictly follow the traditional best practice, avoid magic strings. Magic strings means 

    writing something in Double quotes.

 

9. If there is an action method which returning same result many times, apply caching into 

    it.

 

10. Action methods with HttpGet attribute attached should not be performing an

     operation.

     Example: it should not delete, update any record. In short Get methods should perform

     anything which looks like Post.

 

11. Avoid putting hardcoded Action URIS.

     Example: 

     Instead of  

                Html.Form(“~/MyController/MyAction”,FormMethod.Post”)

     Use

               Html.Form(“ActionName”,”ControllerName”,ForMethod.Post)

 

12. Always include bundling and Minification in your application.

 

13. All your view should be strongly typed

 

14. Use Model Binder for updating Model class object with posted data instead of manual 

      update.

 

Advertisements
By Sriramjithendra Posted in MVC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s