What is MVC? Advantages and Disadvantages of MVC

By on October 28th, 2016

Before 1990, it was not so important to create applications with specific architecture. Since then, applications are getting complex each and every day with protection from new threats that can exploit the application. Also, as the world of programming is getting bigger day by day, smart people are inventing new ways to create applications that can help us create applications fast and easy. There is one such architecture called MVC that was invented in 1970s by Trygve Reenskaug.

In this guide, we will learn all about MVC. We will learn it’s advantages and it’s disadvantages. We will also learn how to work with an application created in MVC architecture. And also, which frameworks are available in which programming language that uses MVC architecture. By the end of this guide, you will know how to work in an application made with a framework that follows MVC architecture. First of all, let us learn what is MVC.

What is MVC?

MVC stands for Model-View-Controller. It is an architecture or a software design pattern that makes creating huge applications easy. It does not belong to specific programming language or framework, but it is a concept that you can use in creating any kind of application or software in any programming language.

For example, if you are developing an application in PHP, you can use frameworks like Laravel or Codeigniter that uses MVC architecture to help you develop applications fast and simple. It might be a little hard to get your head around the MVC structure at first if you have created very simple applications without using any kind of architecture or framework. But, this guide is to explain you how to work on MVC architecture. Once you will understand how it works, you will just love working on MVC.

Here is how MVC architecture works in a simple and easy to understand illustration.

MVC architecture

The architecture we are talking about right now contains three different parts. The different parts are Model, View and Controller. Each and every one of them plays an important role in application development. Let us learn about these three parts of the architecture in detail.

What is Model in MVC?

Model works directly with the database. It does not have to deal with user interface or data processing. In real world scenario, you will simply use model to fetch, insert, update and delete data from your database.

To explain it practically, imagine we are creating a task management application that will simply allow user to organise tasks based on date and time. It means that we will have users and tasks to manage in our database. In the language of MVC, User and Task are models in our application.

So, what we will do is we will create two models in our application named User and Task. Note that models also have relationships with each other. In this case, Every task belongs to a specific user, and a user might have multiple tasks. So, we will have one method in our User model to fetch all the user’s tasks and we will have one method in our Task model that will fetch a user.

In addition to that, we will also have some methods in both the models like Create, Update, Delete in our models that will simply delete a record from our table. For example, if you want to create a user account, you just have to run the following code.

$user = User::create([
    'firstname' => 'John',
    'lastname' => 'Doe',
    'email' => 'johndoe@example.com',
]);

This code follows PHP syntax but you can do the same thing in any programming language. Now, Let’s say user requested to change the name in our database. Here is what we will do to update our model.

$user = User::where('email','johndoe@example.com')->first();
$user->firstname = 'John2';
$user->lastname = 'Doe2';
$user->save();

And to delete a specific user, we just have to use the Delete method of our model which will delete the user account along with all the task that a user has on our system (Foreign key). To show all the data, we use MVC architecture View in our application.

What is View in MVC?

In simple words, View is the User Interface on which our customer/user can perform some actions. It contains HTML,CSS,JS, XML or any other markup language that we can use to create a beautiful user interface. It also contains code to show the data that it receives form our application.

The only two things that a View has to do is to show data to the customer/user on User Interface and to respond to the events. For example, What to do when a user clicks on Update or Delete button? The answer is, user should be redirected to the Update form or to the delete confirmation popup.

Now, It’s time we show our customers/users the task they have in our example task management application. We will show it in tabular format. Right now, we can fetch all the tasks of a specific user using our Model. And somehow, Let’s consider that we have all the tasks of a specific user in a $tasks variable.

To show the data, we will simply create a view with all the HTML markup. And we will run a for or foreach or while loop to iterate through the $tasks variable. And, we will also put the links to Update and Delete URLs. The view in account is called Index view. It will only show the list of tasks in tabular format. We will also create Create and Update views for our Task model.

Similarly, Register is a Create view for our User model. And the list of all the users in our admin panel is the Index view of our User model. So, In short, View is the user interface that displays data and sends the events like update, delete and create to the respective controller.

What is Controller in MVC?

Now comes the most interesting part of the architecture, Controller. Controller is the part in which we process the data after we get a request from View and before updating anything in our database with our Model.

Imagine, the homepage of our application prompts user to enter three tasks that he wants to manage in our application. Once he enters those three tasks, he is redirected to the registration form along with these three tasks. And after registration, those three tasks will appear in the dashboard.

Controller makes it easier for us to manage such scenarios. It is because Controller contains the functions that we can program however we want. For example, After receiving the data in our UserController in the Store method, we will simply create a user account. After creating a user account, we will check if there are any tasks that a user has submitted along with the name and email.

If user has submitted the tasks, we will also create user’s tasks before returning the “Registration successful” message to the Register view.

In our task management application, we will have two controllers named UserController and TaskController. Controllers contain the methods like Create, Update, Destroy, Store and Show. In case if user accesses the Index or Show or Update view, it will simply fetch the data from model and return it to the View in a variable.

Now, Let us discuss the advantages and disadvantages of MVC architecture.

Advantages and disadvantages of MVC architecture

There are few advantages and disadvantages of MVC architecture as every architecture has. First, Let’s see it’s advantages.

Advantages of MVC architecture:

  • Development of the application becomes fast.
  • Easy for multiple developers to collaborate and work together.
  • Easier to Update the application.
  • Easier to Debug as we have multiple levels properly written in the application.

Disadvantages of MVC architecture:

  • It is hard to understand the MVC architecture.
  • Must have strict rules on methods.

There is not much in the disadvantages part of the architecture. And the disadvantages are not so huge and are very easy to ignore in comparison with all the benefits we get.

If you have any questions regarding MVC, please feel free to use the comment section given below. We are happy to help!

One Response to “What is MVC? Advantages and Disadvantages of MVC”

  1. muyiwa Adewale says:

    Thanks for this information. A quick question, please. What are some ways the MVC can change the user interface? Can you share some examples, please?

Leave a Reply