MVC Pattern in C Sharp

Author - Webner
21.01.2022
|
0 Comments
||

Introduction

MVC stands for Model View Controller. It is a software architectural pattern that splits the application into three parts: model, view, and controller. The model is the data, the view is the user interface, and the controller connects the model and view. This pattern helps to achieve separation of concerns i.e. each of these components is separated into different objects.
User sends an HTTPS request that is routed to a Controller which further works with the Model to perform user actions and/or retrieve results of queries. The Controller then chooses the View to present it to the user and provides it with the required Model data.

Below is the UML Diagram for MVC Design Pattern.

MVC Pattern

Both the view and the controller depend on the model but the model is independent of both view and the controller. This separation of concern allows the model to be built and tested without depending on the visual presentation.

Let us understand each of these design components one by one.

  • Model: The Model in an MVC application represents the C# classes that hold the data that our application needs, and the business logic that operates on that data. Model classes are found under the ‘Models’ directory.
    For example, a model class for a student data might look like this:

    modal

    It is independent of the user interface. It directly manages the data, logic, and rules of the application.

  • View: Views are responsible for rendering content in the form of an HTML page to the user. It represents the HTML that is sent to the user and displayed in the browser. This HTML is not static. It is generated dynamically by the controller using the model data. Razor view engine is used to add .NET code in HTML markup. The views are stored in a .cshtml file and are located under the ‘Views’ directory.
    For example, a view to render Student data might be:

    view

    The View presents the model data to the user. The view knows how to access the model’s data, but it does not know what this data means or what the user can do to manipulate it.

  • Controller: The Controller exists between the view and the model. These are C# classes that connect a model and a view. It handles user interaction by listening to events triggered by the view, working with the model, and finally passing it to the view to dynamically render a response. The controller classes are stored under the ‘Controllers’ directory.
    For example, A StudentController that builds the view for the student by fetching the Student data will be:

    controllers

    In the MVC pattern, the controller is the starting entry point and is responsible for selecting model types to work with and view to render. Hence, it controls how the application responds to a given request.

Advantages

  • The main Advantage of MVC is that the separation of concern means the application is divided into Model, Controller, and View.
  • Easy for multiple developers to collaborate and work simultaneously on the model, controller, and views.
  • Models can have multiple views.
  • Development and maintenance of the application become fast because of separation of concern.
  • Debugging becomes easy as now we have multiple levels properly written in the application.

Disadvantages

  • The framework is a bit complex as it has multiple layers of abstraction.
  • Developers using MVC must have knowledge of multiple technologies.
  • It is not suitable for small-level applications.
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.