DEV Community

Leo Barnuevo
Leo Barnuevo

Posted on

What is MVC and how can it make your work easier?

Designing and programming an application or web page, is a task that requires a lot of attention to detail to achieve good results in an optimal and orderly way, which guarantees us the effects we want, while having a good pace of work that makes all the activity pleasant and not complex.

As developers, we have several tools that help us perform our tasks in a simple and fast way, one of these is MVC, a software architecture pattern that allows us to work the code in a much easier way.

In this article, we are going to define what MVC is, what it is for, some practical examples so that you understand it and how it can help us improve our work processes.

What is MVC?

MVC is short for Model, View, and Controller. It is a software architecture pattern that allows us to separate the code according to the responsibility of each component, by doing so we achieve an organized structure in layers where every part is in charge of something in particular, they are ordered in model, view and controllers. In practice, this means that when we make a change in one part of our code it does not affect another part of it.

When developing our product it is necessary to do it in a systematic and tidy way, so we not only ensure good results, but also optimize our work process, and respond to certain desirable parameters for all development such as ease of maintenance, reuse of the code and having each function separated, thus the process responds to certain quality standards through intelligent solutions.

This kind of architecture, is a tool that is available to help us and that we must know to create higher quality applications.

Now let's see how it works, we have 3 parts: the model, the view and the controller and each one fulfills different functions without affecting the others.

Model:
It is responsible for manipulating, managing and updating the data. It contains mechanisms to access information and also to update its status.

View:
It contains the code of our product that will produce the visualization of the user interfaces. The view generally works with the data, but it doesn’t have direct access to it. The views will require the data from the models and they will generate the output, as our application requires.

Controller:
It contains the code necessary to respond to the actions requested by the user through the interface. It is a layer that serves as a link between the views and the models, its responsibility is not to directly manipulate data, or show any type of output, but to receive the user's orders, request the data from the model and pass it to the view.

Generally, this is how it works, but it is important to mention that it has variants when implementing it, to adapt to the needs of every task. In reality, we can see its benefits if we compare it with HTML, where the content and presentation to the user are mixed, so if we want to change, for example, how a page looks, we must change all the files that make up the web.

Here we see the need to separate the elements of our project into parts, not only does it give a professional final result, but it also facilitates the development of the product for us as developers.

To better understand its function, we can list the steps of the process in practice:

  1. The user requests something by clicking on a part of our product, it reaches the controller.
  2. The controller, who communicates with models and views, requests the information from the models and asks the views for the output to show on each screen.
  3. The views can also request information from the models, but for the most part the controller will be responsible for requesting all the data from the models and sending them to the views, acting as a bridge between them.
  4. Views send the answer to the user on the screen. Although sometimes that output can go back to the controller and it would be this one that makes the shipment to the client.

The tools are in our hands, we must work smart and apply them, the greatest benefit is for us since we save time and facilitate the task. I hope this article has introduced you to a new way of working,and if you already knew about it, how much has it helped you to improve your work?

Top comments (0)