Site icon

MVC Design Pattern

What is MVC Pattern ?

MVC stands for Model-View-Controller (MVC), it is a software design pattern that decouples various concerns in an application.

Why MVC Pattern ?

The MVC separates an application into three main aspects:

MVC Pattern

If you look at the above diagram, In a typical MVC design pattern. The end User interacts with the View, which is basically the UI layer. Upon the user action e.a user clicks any button or mouse hover event, the View invokes corresponding Controller. The controller then determines the Model and updates it as per the requirement. Once the Model is updated then the Controller generates the View and updates it for the end user.

Lets look at the example code below, that uses the MVC pattern to display the Employee detail.

EmployeeView.CS – This file has an Interface as  IEmployeeView and a view  class as EmployeeView, that implements the interface IEmployeeView  .

namespace MVC_APP_DEMO.View
{
    interface IEmployeeView
    {
        void DisplayEmploeeView(IEmployee emp);        
    }

    class EmployeeView : IEmployeeView
    {
        public void DisplayEmploeeView(IEmployee emp) 
        {
            Console.WriteLine("Employee");
            Console.WriteLine("*******************************");
            Console.WriteLine("Employee First Name - {0}", emp.FirstName);
            Console.WriteLine("Employee Last Name  - {0}", emp.LastName);
            Console.WriteLine("Employee ID         - {0}", emp.EmployeeID.ToString());
     Console.WriteLine("Employee Salary     - {0}", emp.EmpoyeeSalary.ToString());
        } 
    }
}

Employee.CS – This file has an Interface as  IEmployee and a model as Employee, that implements the interface IEmployee .

namespace MVC_APP_DEMO.Model
 {
     interface IEmployee
     {
         string FirstName{get;set;}
         string LastName{get;set;}
         int EmployeeID { get; set; }
         decimal EmployeeSalary { get; set; }
     }

class Employee : IEmployee
{
    string _fname;
    string _lname;
    int _employeeId;
    Decimal _empSalary;

    public String FirstName
    {
        get { return _fname; }
        set { _fname = value; }
    }

    public String LastName
    {
        get { return _lname; }
        set { _lname = value; }
    }

    public int EmployeeID
    {
        get { return _employeeId; }
        set { _employeeId = value; }
    }

    public decimal EmployeeSalary
    {
        get { return _empSalary; }
        set {_empSalary = value;}
    }
}
}

EmployeeController.CS – This file has the controller as EmployeeController 

namespace MVC_APP_DEMO.Controller
{ 
    class EmployeeController
    {
        private IEmployee empModel; 
        private IEmployeeView  empView;

        public EmployeeController(IEmployee emp, IEmployeeView empView) 
        {
            this.empModel = emp;
            this.empView = empView; 
        }

        public void DisplayEmploeeInfo()
        {
            empView.DisplayEmploeeView(this.empModel);
        }

        public void UpdateEmployeeSalary(Decimal salary)
        {
            this.empModel.EmployeeSalary = salary;
        }
    }
}

Main Method

namespace MVC_APP_HELLO_WORLD
{
    class Program
    {
        static void Main(string[] args)
        {
            IEmployee empModel = new Employee();
            IEmployeeView empView = new EmployeeView();

            empModel.FirstName = "David";
            empModel.LastName = "Brown";
            empModel.EmployeeID = 32567;
            empModel.EmployeeSalary = 90000.00M;

            EmployeeController empController = new EmployeeController(empModel, empView);
            empController.DisplayEmploeeInfo();

            Console.ReadLine();
        }
    }
}

Output

MVC Demo Output

Benefits Of MVC Pattern :

Exit mobile version