Explain the MVC pattern in ASP.NET

ASP.NetServer Side ProgrammingProgramming

MVC stands for Model-View-Controller. It is an architectural pattern for software applications. Trygve Reenskaug came up with the pattern in 1979 for developing interactive applications. In this pattern, the application is divided into three components: models, views, and controllers.

Model

The model maintains the state of the application. The state can be in transient, i.e. in memory, or persistent, i.e. saved to a database.

A model does more than just holding the state. It performs the business logic on the data, enforces the business rules on the data, and can use a domain model to manipulate the data. The model acts as both a gatekeeper and a data store.

View

A view generates and displays the user interfaces based on the data in the model. Upon receiving a request, the model grabs the data from the database or memory, processes it, and hands it over to the view. The view then decides how to render that data to the user.

Controller

A controller coordinates the application flow and acts as a middleman between the view and the controller.

A request is received by a controller. Based on the content of the request, the controller uses the model to build the data and then forwards it to the view, which displays it.

Here is a diagram that illustrates the MVC pattern in the context of a web application. The numbers on the arrows indicate the sequence in which a request from the user is processed.

  • A user interacts with the web application which sends an HTTP request to the application. This request is intercepted and processed by the controller.

  • The controller uses the data from the request to invoke the domain model.

  • The domain model grabs the data from the database (or, from external resources), enforces the business rules and processes the data according to the business logic

  • The processed data is returned to the controller.

  • The controller passes the data to the view. The view generates the user interface using the HTML templates and the data.

  • The view then sends the generated HTML to the web application, which is then displayed by the browser.

The MVC pattern ensures the principle ‘separation of concerns’. For example, the view never handles the incoming data, the model doesn’t concern itself with how to display the data, and the controller doesn’t perform any business logic. Each component has its well-defined responsibility. This makes an application easy to develop and maintain over time as it grows in complexity.

Originally, the MVC pattern was developed to ease the development of conventional GUI applications running on desktop. It later found its use in web applications. The framework Ruby on Rails popularized the MVC pattern, and it was later adopted by other frameworks.

In an ASP.NET application, the MVC components are handled by C# classes. For example, here is a model class representing a user.

Example

Live Demo

// Models/User.cs
namespace app.Models{
   public class User{
      public int ID { get; set; }

      public string Name { get; set; }

      public string Salary { get; set; }
   }
}

A view to render a user might look like:

// Views/User.cshtml
<div class="user">
   <div class="name">
      <a href="/users/@user.ID">@user.Name</a>
   </div>

   <div class=salary>
      @user.Salary
   </div>
</div>

Finally, here is a controller that builds the view for the user by fetching the user model.

// Controllers/UserController
namespace app.Controllers{
   public class UserController : BaseController{
      public IActionResult User(int id){
         // Get the user from the database
         User user = _service.GetUser(id);

         // Render the user.cshtml view, by providing the user model
         return View(user);
      }
   }
}
raja
Published on 22-Jun-2021 13:43:39
Advertisements