What is MVC Pattern ?
MVC stands for Model-View-Controller (MVC), it is a software design pattern that decouples various concerns in an application.
- It is a powerful and effective way of designing applications that separates the UI (User Interface) logic from the data access and data manipulation logic.
- It’s explicit separation of concerns adds some extra complexity to an application’s design, but it provides enormous benefits to the application’s stability, functionality and testability.
Why MVC Pattern ?
The MVC separates an application into three main aspects:
- Model: A set of classes, which are basically the data you’re working with along with the business logic and rules that describes how the data can be manipulated.
- View: It defines the UI of the application, in other words it’s the representation of the data that model contains.
- Controller: A set of classes that handles user input and acts upon the model to generate required view.

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 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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

Benefits Of MVC Pattern :
- The separation of concern in MVC pattern allows multiple developers to work simultaneously on the view, model and controller, which ensures faster development.
- In MVC pattern , we can develop multiple views for a model.
- Source code duplication is very limited in MVC, because it separates data and business logic from the display.
- MVC pattern ensures cleaner and bug free code.
- It allows code re-usability and enables Test Driven Development(TDD).