25 Oct 2017

What is this pattern called MVC?

Well MVC stands for Model, View, Controller words.

Ok, so MVC is a design pattern as I mentioned before, well what I meant when I mentioned the Model, View, and Controller is that we have the (View) here we refer to the Interface or UI, about the (Model) we are talking about the data, and the (Controller) is making reference to the application logic.

Originally this pattern was designed to build desktop applications on the 70s this pattern was pretty well adopted as an architecture for Web Applications, and many frameworks now use this pattern on they architectural way of doing development, here I am focusing more on ASP.NET MVC 5.

When used this MVC pattern for websites, we make the request routing to the Controller this one takes the responsibility to work with the Model and implement the actions programmed or also to take the data, then the Controller selects the correct View to display this data, and provides the Model, finally the view renders everything we programmed to be sent based on the data of that specific Model.

Because we will focus on MVC with ASP.NET, I will talk about the implementation and give a small example making use of ASP.NET MVC pattern, ASP.NET gives us a pretty good way to build dynamic websites using this pattern, making a clean separation of concerns.

Models & Data

The model is a class that is clean and can easily be bind to most databases, we just need declare the validation rules, using C# attributes, of course this can be applied to the client and the server.

Here we can see a Model called Customer, models are just classes that has pretty descriptive properties and methods, this properties has the capacity of getting the values already assigned to them and send them to de View via the Controller and also the attribute to set a value that makes possible to send a specific value if we want to assign to it through the view.


Controllers

The Controller simply route the requests to controller actions, the implementations is made with common C# methods.

Here we can see the Controller, it is responsible for handling the Http requests. It is used to define and group the set of actions. They are instantiable classes and they inherit from a class whose name is suffixed with "Controller"... of course.


Views

The View is the HTML Markup that we use to display the data to the user.

The data on the view can be rendered in many ways, here we can see that we are using an ASP.NET programming syntax called Razor, is a combination of C# and HTML is a clean and lightweight way to render HTML content based on your view. With Razor you can render a page using C#, producing fully HTML5 compliant web pages.



Conclusion

We know that MVC is a pattern that was originally designed for desktop applications and adopted for Web Applications.

Personally, I really like this pattern there are other patterns for web development, like, Model View Presenter (MVP) and Model View ViewModel (MVVM) but they are out of the scope of this document, so if you want you can take a look to them MVP, MVVM.

MVC pattern is quite nice because the business logic is not implemented on the UI, it is pretty easy to make Unit Test with it, everything is cleaner due to separation of concerns, it is Extensible and Pluggable. Of course, it implies a little more work and effort to implement because everything is on the correct place, and the control of the Logic has to be pretty well determined.


Tags: