Understanding the What and Why of the MVC Pattern
page 2 of 3
by Brendan Enrick
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27005/ 35

Aspects of MVC

The obvious thing to wonder is what the acronym even means. It is not easy to tell. A lot of people know what it stands for, but not what it means. The first thing I want to do is explain this.

We have a few pieces I will be explaining. There is obviously the Model, the View, and the Controller. One piece a lot of people forget about is the infrastructure. This includes any external resources you might access: databases, files, RSS feeds, etc. Even though the acronym goes MVC, for the purposes of this article I will be going View, Controller, and then Model.

View - What

As anyone who has ever tried to test a web application knows, it is difficult to test the UI of an application. It is easy to test a method with few dependencies because you can easily just call the method and check the results. With web pages it is quite difficult since you would need to check what html the page is generating. In the case of MVC, the view is not very different from the web forms version. The difference actually comes in how people use them. These views for the most part need to only know about displaying information. Decisions should not be made on the UI. Those decisions should be made elsewhere. Even decisions which will affect UI should be elsewhere and just called from the View.

View - Why

This is a huge advantage to us because we no longer have much to test in the UI. This makes it a lot easier to test the UI because it is a lot less complicated. This makes our tests easier and also has a lot fewer tests we will need in the UI.

Controller - What

The controller is exactly as its name states. The controller takes control of the site. This is where all of the actions logic exits. Actions which users may take using the site are exactly what you will find in the controller. The pages, which users can view, are in here. Forms around the site will post back to these actions in the controller. This is a lot like what used to exist in the web forms. This logic would be contained in the code behind files of the pages.

Controller - Why

As we mentioned before about the view, user interfaces are hard to test. I have also mentioned that we kept a lot of this logic with the UI in the past. By keeping this completely separate the whole time, we are able to keep the view clean of this logic. The controller is actually pretty easy to test. Actions in controllers receive information from the forms when users post to the site. Thanks to bindings, the methods for the actions take parameters which are used to get the data from the user.

When we test these we can pass in the data we want to test as parameters and we are able to look at the result returned from the controller. This allows us to very easily and effectively test the controller and its actions.

Model - What

The model is the connection between the application and the data in the infrastructure. In the model you will define all of your domain objects. This is where entity classes will exist. These classes will define your data. These will include properties of different types as well as probably some logic specifically for working with these classes.

When data needs to be shown to users, it is obtained in the controller and this modeled data is then passed from the controller to the view. The model is where the application is going to get its data.

Model - Why

It just makes sense to keep the data objects and the logic which works with them separate from the rest of the application. I am sure if you know about unit testing, then you know all about the idea of breaking dependencies. Keeping these objects nicely separated allows you to test them more easily.

View Entire Article

User Comments

Title: What's the difference with the three tier logic?   
Name: Yas
Date: 2009-01-20 4:46:17 AM
Hi Brendan, very nice article!

I was just wandering what's the difference between the MVC pattern and the old three tier logic - UI Layer / Business Logic Layer / Data Access Layer ?

Best Regards,
Title: RE: Rubbish...   
Name: Brendan Enrick
Date: 2009-01-19 8:33:08 AM
@Muhammad Adnan I am sorry you did not like the article. The reason I did not write about MVP is because it is in fact a different pattern. While a lot of people note the similarities between the two patterns they are in fact two different things.

Since I was writing about MVC, and not MVP, I decided not to bring up the topic of MVP at all as I did not want to confuse the readers.

Please keep in mind I wrote this article as a way for people who were confused about MVC to understand it better, so adding in unrelated information would have simply created extra confusion.

Thank you for your comment. I hope you find my next article more interesting.

Title: Thank you   
Name: Brendan Enrick
Date: 2009-01-19 8:29:08 AM
@hanife @saanj @ Puneet thank you for the comments. I am glad you liked the article. I am hoping I get the chance to write some more articles soon, so please check back here.
Title: MVC   
Name: hanife
Date: 2009-01-19 5:55:04 AM
Great artical..
Title: Rubbish...   
Name: Muhammad Adnan
Date: 2009-01-19 2:22:12 AM
Didn't describe MVP at all and somehow prolonged unnecessarily....
Title: Dev   
Name: Saanj
Date: 2009-01-18 11:48:03 PM
It is a very useful article which really helps a guy like me who never worked with MVC Architecture. Great article/
Title: feedback   
Name: Puneet
Date: 2009-01-18 11:24:08 PM
Hi Brendan,

This is a nice and simple article. I look forward to more articles from you on how one can implement the MVC framework in applications. A few articles from you on the ASP.NET MVC Framework would also be great for the readers!


Title: Excellent Article   
Name: Sunil Punjabi
Date: 2009-01-03 1:49:57 AM
Excellent piece of information. Keep it up and post some more.
Title: gr8   
Name: Ravi
Date: 2008-12-16 3:17:30 AM
it's really a great article abt MVC in easy words.
Title: another example of wonky behaviour   
Name: gerry lowry
Date: 2008-12-03 6:12:29 AM
I notice nothing by "Enter Comment" below BUT
if I e-mail this page to myself, in Outlook Express
I see "Please and 3 and 3 and type the answer here:"
above the "Enter Comment" button and a text box to the left
of the "Enter Comment" button.

Gerry (lowry)
gerry dot lowry at ability business computer services dot com
Title: ASP.NET MVC is only a minor part of the solution to wonky behaviour   
Name: gerry lowry
Date: 2008-12-03 5:48:21 AM
This article itself is at least for me a perfect example of the problems one encounters. I'm not talking about Brendan's words. Brendan could have choosen random words from random languages for that matter.

Specifically, I'm talking about the testing aspect. As developers, we often get many things for free. For example, the text box in which I'm typing this message has undo available via Ctrl+Z.

Likewise, in the article itself, I can double click to select a word and triple click to select a paragraph.

BUT, yes BUT, no matter how fast I click the above paragraph found between "Model - Why" and "Conclusion"
("It just makes sense ... to test them more easily."), I only get double click behaviour.

This behaviour happens in IE7 but NOT in Safari 3.2.1.

We need testing tools that can ferret out wonky behaviours like the one I just mentioned. Fortunately, this one's not critical. OTOH, critical or not, there's a bug here. It could be in my IE7 or my Win XP Pro SP3 or ... or a combination of everything. I'm curious but I've no time to dig further at the moment, so I'll just file this under mystery.

imo, the most important aspect of MVC as implemented in ASP.NET MVC and as can also be implemented in other ways, is the testing aspect. For that you require a compatible testing frame like xUnit, NUnit, et cetera, as well as related technologies like MoQ, WatiN, and so on. Why? Because among other things, ASP.NET MVC is a Beta product built on .Net 3.5 ~~ as components change, you'll need to test and retest. With automated tools, you can keep your ASP.NET MVC application as healthy as possible and when it becomes ill, you can diagnose it and cure it faster. Most important, when a new component is released, through automated tests, we have better than half a chance to find and repair before our end users inform us about unpleasant surprises.

Regards, Gerry

P.S.: thank you Brendan for your article.

Link: http://www.asp.net/mvc/
Title: RE: Straight to the point   
Name: Brendan Enrick
Date: 2008-12-02 1:17:12 PM
@Joe Thanks for the comment. That is exactly what I was hoping to achieve with this article. I am glad this worked well for you.

Plenty of people asked me what the big deal with MVC is, so I wrote this to give a simple to understand explanation of MVC.
Title: Straight to the point   
Name: Joe
Date: 2008-12-02 10:40:35 AM
thank you for this article, i haven't had time ot wade through all the MVC hype, so an simple, straight to the point article was just what i needed!
Title: Mr.   
Name: Joydip Kanjilal
Date: 2008-12-02 2:03:54 AM
Hi Brendan,

This is a nice and simple article. I look forward to more articles from you on how one can implement the MVC framework in applications. A few articles from you on the ASP.NET MVC Framework would also be great for the readers!



Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2023 ASPAlliance.com  |  Page Processed at 2023-09-25 9:21:06 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search