Today we’re going to take a look at an explanation of the MVC (Model-View-Controller) pattern or architecture, including its characteristics, benefits, and of course, its drawbacks.
Index
1 - What is MVC?
MVC is a design pattern or architectural approach whose main goal is to divide an application into three components: Model, View, and Controller. The aim of the MVC pattern is to separate the application into distinct sections with clear purposes, so responsibilities are not mixed together.
NOTE: This is one of the few acronyms that are the same in both English and Spanish.
2 - How does the MVC pattern work?
For me, it’s always easier to understand with a use case, and the most common is on web pages , for example, this very blog:
When you load the page, what you’re really doing is making a call to a Controller (the C), which contains the information about the route you’re calling. It only acts as an intermediary between the model and the view.
The controller takes the information and gives it to the model, which is responsible for the logic related to data. Yes, it’s the model that handles things like accessing the database, inserting data, deleting, validating, etc.
This is an important point, because in many applications logic is implemented in the controller, which isn’t quite correct. To be honest, you really shouldn’t put business logic in the controller at all, but that’s another topic.
Once the model has finished working with the data, it returns that information to the controller.
And the controller then uses the view to present the information to the user.
What happens behind the scenes is that the view is a template where we render the HTML based on the data that has been passed from the model to the controller.
What is clear from this diagram is that all interactions between the model and the view take place through the controller, completely separating presentation from data manipulation. In theory, this simplifies the process of building applications.
3 - Benefits and drawbacks of the MVC pattern
For me, one of the main benefits of MVC is that it’s been in use for 40 or 50 years or more. This means not only are there many applications that use it, but it’s also generally what’s taught in schools and universities.
This pattern also introduces us to layer separation. Even though the number of layers is limited, becoming accustomed to this kind of structure is very helpful for your professional career.
This limitation becomes clear especially when you have a large app that has grown from simple logic to something more complex. You’ll realize it gets harder to maintain. Splitting the model into more layers can make the app easier to maintain.
As I see it, that’s relevant for large applications. For proof of concepts or smaller apps, MVC is perfect.
4 - MVC in C#
When it comes to C#, the model-view-controller architecture is very popular. In fact, when you install Visual Studio and select the ASP.NET and web development workflows, it comes with example projects, and one of them is MVC.
I didn’t have it installed, so I’m installing it now…
But just so you know, you can work without the workflows – you just need to know how to do it. With the workflows or templates, things are already set up for you.
Once installed, when creating a project in Visual Studio, Rider, or even from the command line, you’ll have the MVC template available:
And that’s it, here you have the code with the structure, just hit play to run the code:
If there is any problem you can add a comment bellow or contact me in the website's contact form