DEV Community

loading...

All about MVC

akashshyam profile image Akash Shyam ・2 min read

What is MVC?

MVC stands for Model View Controller. MVC is a pattern that is commonly used for developing applications. This pattern allows the developer to compartmentalise various parts of application logic which helps in maintaining cleaner, sustainable and organised code.

Model

The model is the part of the application that deals with all the data. For example, on a server side API, the database schema, model etc come under the "Model" part of the application. Quoting wikipedia -

The model is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application.

View

The view deals with the user interface(text, images, videos etc). It's main job is to present the data to the user. An important principle of the MVC pattern is that the view is completely ignorant about the model. Views are often called dumb things/objects.

The view is the representation of information such as a chart, diagram or table.

Controller

The controller is the bridge between the Model and the View since the Model and the View are completely ignorant of each other. The controller deals with all the application logic. The model and the view should never communicate with each other.

Accepts input and converts it to commands for the model or view

An easy diagram to help visualise this

Alt Text

Advantages of MVC

You might be thinking, all this is great but why should I use it? This is why -

  1. Clear separation of logic
  2. Projects are easier to architect and plan
  3. Faster development process as it is easy to keep track of where code is

Downsides of MVC

These advantages are only a part of the story, a lot of the code we write does not belong in the model nor the view. You might read this and think that yah, no problem just dump it in the controller. Problem solved. Not really. After a while, you will end up with huge controllers with thousands of lines of code which will become imposible to read or test and we'll end up with a colossal mess.

The solution

Recently, developers have come up with a new pattern which is both similar yet different than MVC. It is the Model View View Model. I'm going to discuss this in my next post.

Discussion (0)

pic
Editor guide