Geeks With Blogs
My Blog

While my home development computer is fried I’m going to hack away at my backlog of non-code related posts. Today I’d like to discuss some of the advantages and disadvantages of using Microsoft MVC over WebForms.

First of all, MVC is not the end all, magical platform that many people would like you to believe. In fact I wouldn’t even recommend it to beginner developers. However, if you have experience with .NET and you’re willing to put in the time to learn how MVC works, you will find that it offers much more control than WebForms.

Stateless Development

The biggest difference between WebForms and MVC (and one that is often overlooked) is that MVC is a stateless framework. WebForms (and the associated page lifecycle and viewstate, etc) was designed to bring the familiarity of stateful development found in WinForms to the web. Unfortunately for Microsoft, the web is inherently stateless (each HTTP request is independent from any prior request) so a layer of abstraction was introduced to fudge a connection.

Because MVC does not have any abstraction over a raw HTTP request, there are no Page_Load methods or event handlers. Instead, every request is routed to a method called an action. An action may return a static page, a page with data, raw data (xml, json, csv, etc), modify your model, or pretty much anything else you can think up.

Action Filters

Action filters are truly the most powerful part of the MVC framework. WebForms has nothing similar. Action filters allow you to intercept requests before and after the controller action executes. Filters make up probably 80% of the functionality in my applications. Some things I use them for are:

· Authorization/security checking

· Sorting and paging data sets

· Injecting static data (so I don’t have to manually populate my drop downs)

· Injecting menus into every page

· Input validation

· Workflow control (after saving on an edit screen I auto redirect back to the list page)

· Exception handling

· Dynamically associating an action with a view

· Data transformation (raw data collections to json, csv, xml, pdf, etc)

Action filters are basically unlimited in what you can do with them. I’m sure developers will be finding new uses for them all the time. Check out Wes McClure’s blog for some interesting ideas on automating the use of filters.

Model Agnostic

MVC doesn’t care how you create and persist your model. You can use LinqToSql, Entity Framework, NHibernate (my favorite ORM), raw SQL (please don’t!), web services, or anything really. There are no controls tying you to a specific Microsoft data technology. I’ve spent too many days hacking apart ObjectDataSources to work with anything besides a dbml.

Automated input mapping

MVC provides model binders which convert incoming request data (query string or form posts) into whatever type of object you specify. Many examples online demonstrate binding directly to your data model however you should be aware of the associated risks. MVC will not stop you from exposing dangerous security holes. This is something many WebForms developers are not used to thinking about. Don’t be scared by this though, there are several best practices available to eliminate these risks.

Full control over rendered output

When you create a page (called a view) in MVC, you are dictating the raw HTML output. The default syntax is very ASP-like, scattered with many beestings. (<% … %>) Because MVC was designed MVC to be highly modular you can swap out the default ASP.NET view engine with one of the many other available engines. I prefer the Spark View Engine because it provides a very concise HTML-like syntax. It also allows you to write macros which are HTML based scripts that reduce the amount of markup you need to write.

MVC also provides Html Helper methods, which reduce the need to write common HTML tags. Some examples are Html.Button(“Save”) and Html.TextBox(“name”, Model.Name). Html helpers are simply extension methods so it is very easy to write your own.

No Surprises

The built-in controls in WebForms are guilty of surprising behavior. My favorite example is the asp:Panel control which renders as a div until you set the GroupingText property. Then it becomes a fieldset. Surprise! Hope your css didn’t break.

Convention over Configuration

This is my favorite part of MVC. Although it isn’t available “out of the box”, because MVC is so flexible it is easy to add your own features. I have adapted FubuMVC’s Html convention feature to work with Microsoft MVC. This allows me to have dynamic inputs. Instead of writing Html.TextBox(“name”, Model.Name), I can instead do Html.Input(m => m.Name) and allow the framework to use my conventions to determine that Name should rendered as a text box. MVC 2 will provide similar functionality (with Dynamic Data and templating), but I prefer the Fubu approach because it gives you much more control than even a template would. Expect a post with details about this soon.

True AJAX support

Since actions are not required to return HTML, you can use them to return raw data as JSON for AJAX. MVC even comes with JQuery right in the default project. There is no fake postback, like in WebForms, to render partial views. Since MVC is stateless I use AJAX forms to provide the illusion of state to the user.

Testability

Many people promoting MVC are also promoting TDD and testability. This is because MVC almost encourages unit testing. Unlike many people, I’m not going to encourage testing your controller actions, but the ability to is there. Microsoft listened to the community and provided interfaces for nearly everything in MVC so you can stub away if you please.

Because MVC is not only a framework but also a pattern, it is my hope that developers who are not familiar with patterns will learn more about them while learning MVC. My goal for this blog is to help lower the barrier or entry to MVC with real work examples and to promote pattern oriented development.

This all sounds great, but what are the disadvantages? Businesses that have investments in WebForms may not want to switch any time soon for many reasons.

Small, vocal user base

Although it may sound like WebForms development is over, we MVC developers are still a very small, albeit vocal, minority. Despite being small, it is very easy to find people in the MVC community willing to help you learn. Many members of the MVC team at Microsoft are very active in the community and there is no shortage of bloggers willing to answer questions.

No designer support

Developers who are used to drag and drop probably won’t welcome switching to a framework with no concept of a designer. Everything in MVC is hand written, at least in my experience.

Lack of third party tools

There are thousands of third party tools available for WebForms and many companies have invested in them. The MVC community does not have nearly as many tools available. However there are a lot of free JQuery plugins available for grids, select boxes, calendars, etc. You will find yourself writing a lot of javascript with MVC if you want AJAX. JQuery is one more library that developers will need to become familiar with when switching from WebForms.

I’m sure that there are many advantages and disadvantages that I have overlooked, but I hope this gives you an idea of how MVC compares to WebForms and why you might want to check it out.

Posted on Tuesday, February 23, 2010 11:13 PM | Back to top


Comments on this post: Why you should check out MVC

# re: Why you should check out MVC
Requesting Gravatar...
Nice article.

"In fact I wouldn’t even recommend it to beginner developers"

Actually, I think I probably would. I'd much rather developers learn about HTTP Requests, GET/POST, how to write good html etc. than get sucked into believing that they can build a good web site by simply dragging and dropping a few components onto a page.

IMHO they will be far better armed to deal with all the pitfalls/limitations of web development if they understand what's actually happening.
Left by Jon Hilton on Feb 24, 2010 2:51 AM

# re: Why you should check out MVC
Requesting Gravatar...
Thanks for the article. I am in the middle of reading MVC in Action and you have hit all of the reasons that I am wanting to start using MVC. In the book, they did state that you could use MVC side by side with a ASP.NET Forms application while you migrate. Is that a practical statement? I could see the benefits of a company migrating between the two to use that approach as opposed to the ground up rewrite.
Left by Chuck on Feb 24, 2010 4:44 AM

# re: Why you should check out MVC
Requesting Gravatar...
Thank you so much for this clear, concise and enlightening overview.
Left by Yann Trevin on Feb 24, 2010 5:33 AM

# re: Why you should check out MVC
Requesting Gravatar...
Thank you for the article. Our team is working on a project with probably over one hundred web forms and if we ever decide to go to MVC it would have to be a phased approach. Just to reiterate Chuck's concern, is it really possible/practical to have MVC and WebForms living side-by-side without any side effects. And I'm assuming a lot of the cross-cutting concerns such as Security, logging, etc. can be externalized with MVC.
Left by Wahid Alimi on Feb 24, 2010 8:34 AM

# re: Why you should check out MVC
Requesting Gravatar...
Excellent post; thank you.
Left by Michael on Feb 24, 2010 8:54 AM

# re: Why you should check out MVC
Requesting Gravatar...
I learn best by reading code, any best in breed sites on in a public repository to share some best practices.
Left by John Franklin on Feb 24, 2010 9:48 AM

# re: Why you should check out MVC
Requesting Gravatar...
@Chuck @Wahid It is possible to use WebForms and MVC together. I am currently doing this on two projects, both with about 30-40 views. One of the projects had been developed with the MVP pattern so we had luckily addressed many of the cross cutting concerns in a service layer. I simply had to recreate the site MasterPage in MVC.

On the other project I was not as directly involved with addressing the architectural/security concerns, but I believe we ended up using an adapter for our new service layer to access the old security module. Eventually we plan to replace the old code because we're shifting to a distributed event based architecture.
Left by Ryan Ohs on Feb 24, 2010 9:53 AM

# re: Why you should check out MVC
Requesting Gravatar...
You think third party tools for MVC are lacking? MvcContrib, T4MVC, System.Web.Mvc.Extensibility, a bunch of view engines - NHaml, Spark, NVelocity. A great number of blogs, video tutorials and books.
I don't think there is a lack of third party support. May be big companies doesn't invest much in the framework, but as they understand its strength, I'm pretty sure they will. Just my opinion
Left by Gleb on Feb 26, 2010 2:21 PM

# re: Why you should check out MVC
Requesting Gravatar...
@Gleb, I realize there are a lot of tools available for MVC. I use many of them every day. I haven't looked around much since I started MVC 6 or 8 months ago so maybe there are more available now. I however am however thinking about drag and drop style tools where you drop a control on a page, set the datasource and forget about it. I'd imagine MVC controls aren't that easy since we're working with more than just a codebehind. If I'd known this post would get more than a dozen views I'd have done a bit more research on that point. :/

Btw, I plan to be posting a sample "how I do MVC" in the coming weeks so that will probably clarify where I stand on the framework.
Left by Ryan Ohs on Feb 26, 2010 3:19 PM

# re: Why you should check out MVC
Requesting Gravatar...
I like the article. But I don't think MVC should follow steps of WebForms by using heavy-weight controls like GridView and such.
Left by Gleb on Feb 27, 2010 1:17 PM

# re: Why you should check out MVC
Requesting Gravatar...
@Gleb Me either, but I know there are many programmers who like working with those types of tools. They're certainly nice for quick mockups or pages that don't matter (like administration pages).
Left by Ryan Ohs on Feb 27, 2010 1:51 PM

# re: Why you should check out MVC
Requesting Gravatar...
"First of all, MVC is not the end all, magical platform that many people would like you to believe."

Yeah it is. :) Check out Palermo's prediction about MVC taking over the world. http://jeffreypalermo.com/blog/asp-net-mvc-rc-released/
Left by Liam McLennan on Mar 03, 2010 2:02 AM

# re: Why you should check out MVC
Requesting Gravatar...
@Liam MVC is the future, but just switching from WebForms to MVC isn't going to guarantee better software. You have to understand how to leverage the framework to your advantage. Many people will get away with just writing fat controller actions (just like many event handlers I've seen in code behinds) because they haven't bothered to look into filters and binders. There are plenty examples of this online already. However, programmers that do leverage these, will more than likely end up with much more reusable (and readable) code than if you used WebForms. This is why I'm advocating for MVC.
Left by Ryan Ohs on Mar 03, 2010 10:36 AM

Your comment:
 (will show your gravatar)


Copyright © Ryan Ohs | Powered by: GeeksWithBlogs.net