What is MVC?

by Sep 17, 2019Sitefinity Developer0 comments

Hey guys, welcome back. Craig here from Code Engage. Today we’re going to talk about model view controller, or also known as MVC.

So what is MVC? MVC stands for model view controller, and in today’s video, we’re going to talk about what are the three pillars of an MVC model. In a traditional webpage, users will access a webpage and content will be returned. If you’ve been doing any kind of web development up to this point, you know that a webpage is made up of HTML, JavaScript, CSS, images, and so on. You might even have PHP or anything like that. When you go to access that web page, it returns the page and it renders the PHP and delivers the page to you.

The Controller

So now I’m going to describe a newer model. So if you go to some sort of domain, in this case, somedomain.com, it’s a fictitious domain. When you access that, you’re accessing the controller. So in a normal concept, you’d be accessing a webpage. In asp.net, you’re essentially accessing logic initially, the controller.

Think of the controller as the brains. It’s going to figure out what to do next. Now, this might seem kind of odd, because normally when someone is requesting a domain or a webpage, they’re going straight to a webpage, and it logically seems correct except this concept gives you more control over what is delivered to the user. Now the controller is the brains behind this. It determines what do I do next.

The Model

Let’s say, for example, we’re going to display a list of products. Okay, so I have to go get data. So the model then says, Hey, here’s where the data’s at. I’ll get it to you and I’ll return it back to the controller. So now there is this separation of concerns. The model goes and gets the data, the list of products, and returns it back to the controller. The controller really doesn’t know how it got the data or where it got the data from, but the model will get the data.

The View

Once the data is returned back to the controller, the controller can then decide how to render it. The controller can then pass the data off to a view. The view is what renders the data. So now you have these three pillars; controller, model, view.

Now, the view displays the content. It does not contain any business logic, does not contain any sort of way for you to control what’s getting displayed. The view just knows how to display the data.

The controller knows how to go get the data and return it to the view and the model knows how to go get the data. So this concept of model view controller separates these different concerns, the doing from the getting, from the viewing.

Model, View, Controller (MVC) Pattern

Testing

So that concept makes it easier for testing. If I wanted to test the concept of getting the data, then I would just need to concern myself with the model and not with the view. If you are someone old like me and you remember web forms, web forms had this concept of a code-behind page, and that code-behind page was defined in the HTML and was essentially a link to the code behind, a C sharp file, and that made a very tight coupling. But with this concept, we aren’t creating these tight couplings.

Separations of Concern

We are using a more strategic approach to getting data and viewing data and we’re separating those components. Another reason why you might want to use this kind of construct in your website development is that if you are a frontend designer and you know how to create frontend concepts or views and make them look really good, then you can create the view.

And on the flip side, if you’re a backend coder and you know how to deal with data, getting the data, storing it in a database and so on, then you can deal with the model. And then another person, a third coder could write the controller.

So that concept where you can start to break down components and use those independently now becomes a lot easier. So once you have the view, then the webpage is returned. So from the user standpoint, the data, logically, it seems very similar to a normal webpage, but now we’ve kind of, as a developer, broken down the page into a controller, a model, and a view. And those concepts now make it easier for different developers to do different parts of this process.

Conclusion

I think it makes it a lot easier. I think it works a lot better this way. This is the future strategy going forward for pretty much most of the Microsoft technologies around web development including Sitefinity development and even other CMSs. I think Kentico has just switched over to this concept, so going forward, this concept of model/view/controller is going to be super important to understand this.

This is a very high-level view, and hopefully, in future videos, I’ll be able to show you how to create this kind of code and be able to use these kinds of things in a Sitefinity instance and give you the understanding of what it takes to build one of these.

Thanks for watching and stay Code Engaged.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

3 + nine =

In This Post