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

.net 3-tier asp.net-mvc c# entity-framework-6

Question

With Entity Framework 6, I'm developing a new project (web application) in ASP.NET MVC 5. I've worked with ASP.Net web form applications in a three-tier architecture, but dealing with entity frameworks, models, and other concepts is baffling me. So, how can I create a three-tier design in MVC using EF and can I use entity framework with it?

1
3
2/10/2015 12:14:13 PM

Popular Answer

The answer is that you can use a 3-N tier design (or something similar).

An excellent partnership exists between entity framework and ASP.NET MVC. Even with the default ASP.NET MVC template, EF is deployed and utilized for user/role management (identity).

Models, Views, and Controllers make up an average ASP.NET MVC application. Briefly:

  • POCOs are models. The model contains the data and is in charge of ensuring the accuracy of the data. To provide an analogy, this is a component of both the data and application tiers. The data tier also contains the EF database context class.
  • Views are highly typed.cshtml files that create html (have model). This is the presentation tier, to use an analogy.
  • The processing of HTTP requests, obtaining the data model, and providing the model to the view are all the responsibility of the controller. As an example, this is a component of the application tier (business logic).

The controller typically receives a viewModel, verifies it, processes it, and returns an action result (view, partial view, JSON, file, etc.). In the process section, the controller can set up the entity framework database context and retrieve or save data from the database.

The repository/Unit of Work or service pattern is used in many ASP.NET MVC solutions because it's nearly always a good idea to "keep the controller as small as feasible".

An illustration of a common MVC application component for constructing an entity utilizing 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)
}

Models for presentation views

// 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 -->
6
2/10/2015 1:17:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow