How we can create 3 tier architecture in ASP.Net MVC application with EF?

Mitesh Antala picture Mitesh Antala · Feb 10, 2015 · Viewed 21.2k times · Source

I am creating a new project (web application) in ASP.NET MVC 5 with Entity framework 6. I have worked in 3 tier architecture with ASP.Net web form application but i am confusing to deal with entity framework, models etc. So, how can i build 3 tier architecture in MVC with EF and it is feasible to use with entity framework ?

Answer

Viktor Bahtev picture Viktor Bahtev · Feb 10, 2015

Yes you can implement a 3/N tier architecture (or something similar).

ASP.NET MVC has a great collaboration with entity framework. EF even is installed and used for users/roles management (identity) in default ASP.NET MVC template.

A typical ASP.NET MVC application consists of Models, Views and Controllers. Briefly:

  • Models are POCOs. The model encapsulate data and it's responsible for data validity. For analogy - this is a part of application tier and a part of data tier. The data tier include also EF db context class.
  • Views are .cshtml files which produce html and can be strongly typed (have model). For analogy - This is the presentation tier.
  • Controllers are responsible for processing of HTTP requests, retrieving data model and passing the model to the view. For analogy - This is a part of application tier (business logic).

Typically the controller will receive some viewModel, validate it, process it, and return some action result (view, partial view, JSON, file, etc.). In the process part the controller can initialize entity framework context, and gets or save data to database through the EF db context etc.

It's almost always a good idea to "keep the controller as thin as possible", so many ASP.NET MVC solutions use repository/Unit of Work or service pattern.

Example for some typical part of MVC application for creating some entity using services:

Services

// Connect to database through entity framework db context.
public interface IMyService
{
    MyDbContext DbContext { get; set; }

    IQueryable<Something> GetSomething( ... query params ... );
    void CreateSomething(Something model);
    // etc. 
}

Controller

public MyController
{
    private IMyService myService;

    public MyController(IMyService myService)
    {
        this.myService = myService;
    }

    // Showing create/edit form
    [HttpGet]
    public ActionResult CreateSomething()
    {
        // Create Empty view model. new SomeViewModel(); for example.
        // Get some nomenclatures for dropdowns etc. through the service (if needed).
        // return View(viewModel);
    }

    // Processing the post request
    [HttpPost]
    public ActionResult CreateSomething(SomeViewModel viewModel)
    {
        // Check validity of viewModel (ModelState.IsValid)
        // If not valid return View(viewModel) for showing validation errors
        // If valid map the viewModel to Model (through AutoMapper or custom mapping)
        // Call myService CreateSomething method.
        // Redirect to page with details.
    }
}

Model

public class Something
{
    public int Id { get; set; }
    public string Name { get; set; }
    // .. more properties (can be decorated with validation attributes)
}

Presentation View Models

// View model for Something class. Including only properties related to the current UI.

public class SomeViewModel
{
    public int Id { get; set; }
    // .. more properties (can be decorated with validation attributes)
}

View

@model SomeViewModel

<!-- Form -->

@Html.ValidationForModel()
@Html.EditorForModel()
submit button

<!-- /Form -->