Click here to Skip to main content
Email Password   helpLost your password?

Introduction

MVC (Model View Controller) is the new word for ASP.NET developers. Though it's almost 1+ years old (CTP was released on 12th DEC 2007), it started suddenly buzzing the market. Everybody is talking about ASP.NET MVC now. So I thought of putting an article regarding this, hope this will help people who are trying to learn the new framework from Microsoft. Oops... did I say that, is it a framework?? We will see.

Goals

You can find a lot of articles regarding the new framework on the internet. What this article is trying to convey is when to decide to use this framework over existing ASP.NET models (the web forms basically)... a comparison that will help in decision making.

What is MVC??

As I said earlier, this is a new framework provided by Microsoft to design and develop a new generation of web applications on .NET platform. Go to Wiki, Google and you will find that this is an architectural design pattern. A new generation since the model in which classic ASP.NET page work, it’s different from that.

This is a framework methodology that divides an application's implementation into three component roles: models, views, and controllers.

Why MVC, The New Framework?

Now here is the big question that why we need a new framework and architecture altogether for developing web applications. Are we not happy with the more than half a decade proven technology of Microsoft for doing the same work, that’s web forms (classic .aspx pages). Let us see.

Remember the initial days of ASP.NET 2.0 learning, people have the tendency of mixing up the presentation layer with data access and business logic layer. Especially I remember the sqldataSource control embedded to an ASP.NET server control. See this:

Click to enlarge image

Click to enlarge image

Web applications developed this way are quite difficult to maintain. And they are definitely challenging to test appropriately. Oh yes, have you ever tried to test the full ASP.NET application? It's impossible unless you tweak the runtime environment. You can test the classes in your separated BLL or DAL but not the UI. This is just an example and we have a lot more of this kind. So your boss asks you to read about best practices, design patterns so that you can separate the layers properly, hence it can be reusable, testable and maintainable. Read Google Separation of Concerns to understand.

MVC ensures a clean separation between the data model and the user interface in such a way that changes to the user interface don't affect data handling. At the same time, the data model and related access code can be refactored without the need for changing the user interface accordingly. MVC Framework should be considered as an alternative to the Web forms. With MVC, we are back to the era where people used design web pages with HTML only, the stateless behavior of web. In MVC, you can have full control over HTML which is good for programmers. Web forms were popular since they are able to abstract HTML, but that’s an issue when you need to have browser compatibility and integration with JavaScript frameworks.

MVC does not support post back and view state (the culprit behind page performance), it does not consider the URL to map to a particular physical file on server as in case of web forms. It's just a logical one in case of MVC. So in MVC, you can have custom format and the engine will handle it to render it correctly. How does it do that------------for this please wait to check my next article on Httphandlers and how they handle things in MVC-----------for the time being remember that it’s a factory of httphandlers which is centralized and handles the requests. This HTTP handler analyzes the syntax of the URL and maps it to a special server component known as the controller.

Models and controllers are just simple *.cs files containing interfaces, classes, methods……View is the most interesting here, which is pluggable. That means you can have .aspx, .ascx pages anyway for rendering your controls and text, but also you can have your own view engine created to render contain and you can have your file name extensions as *.abcde and this will be taken care of by the httphandler. For example, you can see one screenshot on how we use an XSLT based view engine to render our content. You can have some other view engines here. MVCContrib library contains 4 alternate view engines, Brail, NHaml, NVelocity and XSLT.

Don’t try to understand this. This is what we use in our organization. I will put up an article altogether about how to write your own view engine. That’s kind of hard but before that, it will have a series of articles.

Ok so we were discussing postbacks, view state and rendering engine. In MVC, the default engine is Web Forms view engine, and that’s why you have .aspx and .ascx files in your MVC framework. So you can have server controls in your pages, have event handlers and render contents, then a question, why MVC?? Again. Ok you can have all those but you don’t have view state in MVC for having the state of the server controls. Of course you can overcome that using sessions, cache objects, etc. If MVC does not have server controls, then what? We have only HTML controls. So how do we do RAD (Rapid application Development), as we do in ASP.NET. Ok we have HTML helpers in this case and I am eagerly looking at any third party or Microsoft to come up with some controls for rapid development in MVC framework. Just have a look when you develop applications in MVC in VS 2008. I am here using the example of nerddinner which is written by scott gu and others. He also has a free book which is a walkthrough of the application with code examples. Though the code provided in the book does require some correction so that the app runs correctly in production, it's almost a complete app you can build from scratch on your own. The errors are minor.

This is how one of the aspx pages looks like and it does not have any page behind code (Separation of Layers).

So what do you have generally on the pages. It's nothing but HTML controls and links with parameters. So unlike ASP.NET pages, these pages are search engine friendly and rank well by search engines. Note: MVC does not have page life cycle concept.

Before I go to other topics or conclude the article, let's talk about the TDD (Test driven development) aspect of MVC. As we see, it's difficult to have a test driven development approach for the full ASP.NET application, it's easy for MVC apps. Since all the three roles (M—V—C) have a clear separation of controls, it's easy to test the full application and that too injecting required test data from the application itself. Please Google regarding Dependency Injection framework and mocking frameworks.

What to Choose

Few points which can help you:

Scenarios What to choose?? Remarks
Your developers love drag and drop control from the tool box and raw HTML is a nightmare for them Web forms Classic ASP.NET controls are abstractions of HTML, with “runat” server attribute.
Your Application demands fancy visual controls to display data. Even if it requires something like Gridview Web froms MVC only has HTML helpers. Some of those are:
  • ActionLink — Links to an action method
  • BeginForm — Marks the start of a form and links to the action method that renders the form
  • CheckBox * — Renders a check box
  • DropDownList * — Renders a drop-down list
  • Hidden — Embeds information in the form that is not rendered for the user to see
  • ListBox — Renders a list box
  • RadioButton * — Renders a radio button
  • TextArea — Renders a text area (multi-line text box)
  • TextBox * — Renders a text box

And you don’t have much choice. We never want a for loop around HTML controls to achieve the gridview functionality.

You want to achieve Separation of Controls build into the system/framework and you don’t need to bother MVC SOC is in the architecture of this framework. With web forms, you can achieve SOC but you need to do that on your own with some best practices while writing your code.
You want to test each and every component of your application right from the beginning, without connecting to databases MVC MVC was designed by keeping testability in mind.
Let me know if I missed any major one. Web forms is mature and MVC is young which lacks component model. But needless to say it leads you towards a good code design.

Conclusion

I know this is not all and maybe I have not put the things well. I just tried to give an overview of both the models and few features of those. Hope this helped to find the line between those 2 models. So to conclude, what to choose as that was our goal, you need to decide. If it's a heavily data driven site, then probably MVC may not fit in. But if its data driven where you don’t have fancy requirements for data display or something of a similar kind and for other types of development, MVC is a good choice. But before choosing any one of them, the architect needs to understand the underlying methodologies of both. This is just a simple work to identify those.

Thanks.

Please mail at debiprasad.baral@gmail.com for any feedback, so that this article can be improved.

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSame old biased remarks
Member 1904970
15:35 5 Aug '09  
"Web applications developed this way are quite difficult to maintain." Stated frequently by MVC proponents, but without any evidence. Maintainability is a much more complex issue than simply following a design pattern.

The same old stuff about viewstate being bad. No it isn't. It is frequently a great convenience, and if you prefer to do all the state maintenance yourself you can always turn it off.

"Classic ASP.NET" implies it is obsolete. Better to call it "Webforms". Webforms and MVC are simply alternative frameworks for development, each with it's strengths and weaknesses.
GeneralRe: Same old biased remarks
Jinx101
16:45 10 Aug '09  
I have to agree with you. I rather like the ViewState and it's highly useful if you understand how to use it and when it can cause performance problems. Also, I don't see why you can't have highly tailored HTML with WebForms.. a lot of us web developer/programmers do both. We don't work in huge shops that have the staff to hand off UI to HTML coders and backends to locked in the basement coders. The MVC is just a different approach, and not a bad one. Myself, I prefer the PostBack model simply for the ease of development and the time saved from not having to manage state in a stateless web.
GeneralRe: Same old biased remarks
debiprasadbaral
5:16 11 Aug '09  
I am trying to answer both of the above posts. First of all Viewstate can't be switched off. Even if you turn off for a page still its there. Thats a feature that we are going to have in .Net 4.0 . Security is a issue with view state and for state management there are other techniques like session for example. View states are for small data. Turn off the view state for a page where you have grid view having paging, sorting and displaying thousands of recored, your page load performance improves multiple of 100 in percentages.

Next you overrides the values for the controls you have on the page once u get it from the server. Then why to use the values in viewsdtate and then overrides the values, I would prefer to use a httphandler for this.

let me know if imissed anything.
GeneralRe: Same old biased remarks
Member 1904970
4:30 12 Aug '09  
If you have a page with display-only data, you can delete the Form tag from the page and not have any Viewstate. If you do have data to be posted back, there will be some amount of Viewstate, but it can be reduced to a very small amount. The basic problem is that some developers may be unaware that large amounts of data may be stored in Viewstate for large datagrids, but this is something people learn as they work with webforms. Just because a feature requires some knowledge to use properly doesn't mean it should be declared bad and taken away.
GeneralRe: Same old biased remarks
debiprasadbaral
5:18 11 Aug '09  
I do agree that instead of classic ASP.net we should be calling it simply web forms
RantGood job but I hate MVC
SlingBlade
23:55 4 Aug '09  
You speak of seperation of concerns as being in favor of MVC and say that you can do it with web forms but you have to do it yourself as if doing it yourself is some kind of problem. Coming from a WinForms background I suppose it is just second nature for me to seperate concerns and those that focus solely on ASP.NET web development might lean more towards tying data directly in with the UI etc. The manner in which MVC seperates concerns adds a layer of complexity that simply needn't be there and requires you to follow a clunky pattern that any developer should be able to do better than when seperating concerns on your own. Sure it does add for consistency and if one was to hire a team of developers trained in MVC it might provide a slight advantage to maintainability but nobody in their right mind would seperate the concerns like MVC does on their own.

Another thing that should be included in your table is integration with legacy ASP.NET applications, which is impossible to do with MVC. Honestly, I think MVC should be boycotted for that reason alone.
GeneralRe: Good job but I hate MVC
debiprasadbaral
5:22 11 Aug '09  
Integration with legacy ASP.net can bo deone in a way that just write a simpke module for routing which will handle the incoming legacy urls and output mvc urls and redirect the user to th emvc url, where the user is trying to access the legacy url.

If this is what you called legacy asp.net application then it can be done. Let me know if you want me to put up a simple article with code for this.

But if this is not what u r talking about legacy asp.net apps then let me know what you are trying to say.

thanks
GeneralRe: Good job but I hate MVC
SlingBlade
10:32 11 Aug '09  
So it can be done. There weren't any good resources for how to accomplish it when I first gave it a shot. The case where I could use it is with integrating an open source project done with MVC into a non-MVC application as a subfolder of the site. It is a good sized project and would probably take a lot of rerouting so if you write an article it would be cool if you targeted to that kind of scenario.

The application I am trying to integrate is the Kigg news feed application. I described my previous integration efforts on their discussion board here[^].
GeneralThanks
Hemant.Kamalakar
4:57 4 Aug '09  
Thanks for sharing.
Generalcontrols in mvc
aliascodeproject
6:24 1 Aug '09  
It is true MVC does not have a lot of controls out of the box.
However, I use jquery and jquery ui, and have written a number of html helpers to use jquery ui easily.
Also, for grid I use jquery grid which is many times better and very lightweight for all my grids.
It requires some effort but the rewards are great. jquery grid can get the data using json (which can be very easily generated in mvc using jsonresult), supports all the features you need (paging,sorting,...) and is very extensible.

I'm very happy not having to deal with page life cycles and events, mvc allows me to do all this and more a lot more cleaner... the only disadvantage is you have to deal a bit with javascript yourself; using jquery however, this is a lot easier.

I find it much easier to write a custom control using some jquery, than to write one using the extender framework of microsoft, I find it too complex.
GeneralRe: controls in mvc
Jinx101
16:49 10 Aug '09  
You point out some tradeoffs. When deciding between the PostBack/Viewstate model and the MVC framework it's really about what you value in development (much like the choice between C# and VB). There are advantages to both frameworks and whether those advantages work for your situation only you can know.
GeneralRe: controls in mvc
debiprasadbaral
5:09 11 Aug '09  
True thats what the conclusion says. End of the day the architect needs to decide what really fits in.
GeneralRe: controls in mvc
Jordan Marr
10:07 28 Aug '09  
Agreed.   I have spent way too much time creating and tweaking custom webforms web controls.   It is so much easier to have fine grained control of the HTML / javascript with MVC.   Yes, it can be done with webforms, but the HTML is abstracted so far away that it takes a rediculous amount of effort and knowledge of the page life cycle and custom control building to render some HTML, which should be a very straight forward task.

Jordan
GeneralHtml Helper
fred_007
17:22 31 Jul '09  
You can build your own Html helpers, and it very easy to do
simply create an extension that returns a string.

I use a Html helper for my captcha, for displaying tags, translation , etc ...


Last Updated 31 Jul 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010