Best Practices, Coding Standards & Folder Structure For ASP.NET MVC Project

In this article we will help fresher candidates to understand how to create our .NET MVC project from scratch with use of MVC, Generic Repository and Unit of Work pattern and Entity Framework.

Introduction

In this article we will help fresher candidates to understand how to create our .NET MVC project from scratch with use of MVC, Generic Repository and Unit of Work pattern and Entity Framework. 

Objective of this article

  1. To understand coding standards
  2. How to create project with folder structure.

Everyone must have knowledge of MVC

Nowadays MVC has more value in every field. Every company looking for candidates who have knowledge of MVC. So this is mandatory to learn MVC pattern & do some coding practice so everyone can get a stand in row of crowd i.e. in IT Industry.

Introduction

As per your project requirements project architecture gets changed. Here I am going to show the project folder structure of MVC with some coding standards which are daily used in life of developers.

Note: This is not mandatory to use this pattern for your project also, as this is up to you.

Step 1:

Create one empty project of MVC template using Visual Studio 2013. Create one table in database with primary key using not null. Then right click on our project & add C# class library for Context i.e. in this library we are going to add .edmx. For this right click on this class class library Add, New Item and then select Visual C# template. From Data section select ADO.NET Entity Data Model & select our table with proper database connect.

Now in our another project create the following directory to achieve architecture of project as in the following image:

DataModel.edmx:

Here you will see DataModel.edmx contain DbContext which is instance represents a combination of the Unit Of Work & Repository patterns such that it can be used to query from database & group together changed that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.

Context:

Suppose you are going to use some table for your project then naming conventions are used to understand for which purpose this table is used. Give proper names to your table like User, Product, Client, Reports, UserRole, UserSubscription, Payment, etc. 

Common:

If you are going to use some code for common purpose then add this in common folder like Authentication, Enum, Constant, etc.

Content:

Here you can add your css, fonts, images, PDF files, etc.

Controller:

Here you can add your all controllers with each action method in respective controller. For example, suppose for User Controller you need to write action method for add/edit/delete user, etc as follows.

  1. public ActionResult Index()  
  2.   
  3. {  
  4.   
  5.     return View("_Index");  
  6.   
  7. }  
  8.   
  9. public ActionResult Add()  
  10.   
  11. {  
  12.   
  13.     return View("_Add");  
  14.   
  15. }  
  16.   
  17. public ActionResult Edit()  
  18.   
  19. {  
  20.   
  21.     return View("_Edit");  
  22.   
  23. }  
  24.   
  25. public ActionResult Delete()  
  26.   
  27. {  
  28.   
  29.     return View("_Delete");  
  30.   
  31. }  
  32.   
  33. public ActionResult List()  
  34.   
  35. {  
  36.   
  37.     return View("_List");  
  38.   
  39. }  

Suppose you want to pass values from your ViewModel to action then you can pass as follows:

  1. [HttpPost]  
  2. public ActionResult Add(AddViewModel user)   
  3. {  
  4.   
  5.     return View("_Add");  
  6.   
  7. }  

Many times we use variables, ViewBag, ViewData in our controller first take of naming conventions that this name will be more easy to read & understand the code, for example,
  1. var userRole;  
  2. var userId;  
  3. ViewBag.userRegisterDate;  
Repository:

The repository pattern is an abstraction. It's purpose is to reduce complexity and make the rest of the code persistent ignorant.Adding a Repository pattern on top of this distances you from the features of your ORM. The main reason for using the repository pattern is so that you can use dependency injection and make your code more testable.
 
It involves creating a UnitOfWork class which encapsulates an instance of the DbContext and exposes each repository as a property. Clients of repositories take a dependency upon an instance of UnitOfWork and access each repository as needed through properties on the UnitOfWork instance. 

Resources:

Suppose your application will use in all over world then you can add resource files according to each country like French, Spanish, etc.

UnitOfWork:

Performs set of transactions in single set of action. To a multiple repositories and call Save() for all of them at once, which calls SaveChanges() which will turn the unit of work into a transaction.

If any transaction fails during your Save() call, the operation is rolled back. 

A)   Implementation

B)   Interface

ViewModel:

To create ViewModel with respective action result follows naming conventions. Suppose you want to create CRUD operation for which you are going to use ViewModel to declare properties & constructors then use AddViewModel.cs, EditViewModel.cs, DeleteViewModel.cs, IndexViewModel.cs.

For example:

  1. [Required]  
  2.   
  3. [Display(Name = "User Id")]  
  4.   
  5. public int UserId   
  6. {  
  7.     get;  
  8.     set;  
  9. }  
  10.   
  11. [Required]  
  12.   
  13. [Display(Name = "User Name")]  
  14.   
  15. public string UserName   
  16. {  
  17.     get;  
  18.     set;  
  19. }  
  20.   
  21. [Required]  
  22.   
  23. [Display(Name = "Email Id")]  
  24.   
  25. [DataType(DataType.EmailAddress)]  
  26.   
  27. public string Email   
  28. {  
  29.     get;  
  30.     set;  
  31. }  
  32.   
  33. [Required]  
  34.   
  35. [Display(Name = "Phone Number")]  
  36.   
  37. [DataType(DataType.PhoneNumber)]  
  38.   
  39. public string Mobile   
  40. {  
  41.     get;  
  42.     set;  
  43. }  

Views:

A)   Shared

In Shared folder we declare our _Layout.cshtml page for global use i.e. for use like as a master page.

B)   Views


To create view for respective action result follows naming conventions. Suppose you want to create CRUD operation then use _Add.cshtml, _Edit.cshtml, _Delete.cshtml, _Index.cshtml, _List.cshtml.

Many times we use some event, css to achieve good design with our own style sheet then we use class or id. To declare any class first take of naming conventions that this name will be more easy to read & understand the code, for example,

  1. <div class=”imageProfilePhoto”/>  
  2.   
  3. <img src=”” id=”imgUserRole”/>  
  4.   
  5. <button onclick=$(‘#lnkDeleteProfilePhoto’).click()>  
To declare validation using model we can declare,

  1. <div>  
  2.   
  3.     @Html.ValidationSummary() @using (Html.BeginForm("Add","Home", FormMethod.Post))   
  4.     {  
  5.       @Html.EditorForModel()  
  6.       <br />  
  7.       <input id="submit" type="submit" /> }  
  8.   
  9. </div>  

Summary

This article will help fresher candidates to understand
 how to create our .NET MVC project from scratch.

Hope you enjoyed this one.