Does ASP.NET MVC Let Me Stop Fighting Web Forms?
Don Kiely discusses ASP.NET MVC Web Forms application, which makes heavy use of data-bound grid controls and a little AJAX.
October 30, 2009
Exploring ASP.NET & Web Development
Does ASP.NET MVC Let Me Stop Fighting Web Forms?
By Don Kiely
I've been thinking a lot about Web Forms lately. Inthe spring I began what was a fairly straightforward administrative applicationfor a client to control some of the back-end processes behind their commercial website.The application manages memberships, certain types of product and serviceorders, and other functions. It's an ASP.NET Web Forms application, makingheavy use of data-bound grid controls (using both data source controls andADO.NET code when warranted), and a little AJAX. Fairly typical stuff, althoughsome of the client's quirky requirements made for some interesting challenges!
But even the straightforward stuff was a challengeat times. For example, in a recent column, "SynchronizedDropDownLists", I wrote about the problem of synchronizing two drop-downlists bound to a database. I was amazed that having a bound drop-down list(bound both for the contents of the list and the current selection)synchronized to another drop-down list is not easy in a ListView control. Theeventual solution wasn't complex, but it wasn't easy to figure out and is, atbest, convoluted. And there were many other struggles.
I realized that the problem with Web Forms is thatalthough the straightforward stuff is easy and often code free (not that code freeis necessarily a good thing), if you stray just a little off the main paththings get very complex very quickly. Far too often I find myself fighting thePage life cycle and struggling with events, finding the magic goo to makeeverything work. You can certainly accuse me of being an inept Web Formsdeveloper, but even so this stuff shouldn't be so hard.
On top of this, I've been working with a client forsome years, maintaining their large, complex, classic ASP commercial website. Ijust can't believe that in 2009 I'm still writing VBScript. But they are agreat client, fun to work with, and we're gradually moving them to newer,better, and more secure technologies. Classic ASP uses this ugly model ofcoding that mixes inline, server-side script code (in our case VBScript) withHTML to produce dynamic pages. It was revolutionary in its day back in the lastmillennium but encourages bad practices such as data access code directlywithin the page. It makes for sites that are a nightmare to maintain.
ASP.NET MVC is an attractive alternative to Web Forms,at least conceptually. It uses the proven model-view-controller pattern to getaway from the Page and postback model of Web Forms, giving the developer farmore control over HTML and other design elements. Microsoft released the firstversion of MVC some months ago and recently released a preview of version 2.0 (goto http://www.asp.net/mvc/ for moreinformation).
The big attraction of MVC is that it removes thebarriers between me and what appears in the page. There is less black-boxoverhead that I have little or no control over; there is less fighting the flowof page processing.
I'm a whitewater kayaker and canoer, and one of myearliest lessons was that I don't want to ever fight the river. Even thesmallest boatable creek is too strong for me to win that battle. Instead, Iwant to work with the river to get to where I want to go, whether it's a wave,hole, or across the river. Harnessed well, I can use the river to accomplishalmost anything I want to do.
More and more, with Web Forms I feel like I'mfighting the river, trying to bend it to my will or at least find the magiccombination of event code and property settings to do what I need. With MVC Ifeel like I'm using ASP.NET in more natural ways to accomplish my goals for apage and application. This is a far more pleasant way to develop, even if itmeans a bit more work!
I have to admit that having more control over theHTML in a page is a big attraction to me. Related to that is that it is easierto write clean JavaScript, and with cleaner HTML Cascading Style Sheets (CSS)can be easier to write and more powerful. Client-side AJAX is easier as well. I'dprefer to stay close to these basic web technologies than I can with Web Forms.This is probably not a good thing for developers who don't have a fairly deepknowledge of HTML, JavaScript, and CSS, but these days that knowledge is easyto come by with a little effort.
Most of the discussion of MVC versus Web Formscenters around MVC's better separation of concerns (SoC) and its testability.SoC deals with separating software into distinct features that don't overlap oroverlap only a bit. It isn't impossible to have clean SoC with Web Forms, butMVC almost pushes you in that direction. And, of course, you can fight thesystem and mess up SoC in MVC if you try really hard.
MVC's testability is less of an attraction for mebecause I don't practice test-driven development (TDD). But it is appealingnonetheless because I can easily write targeted tests that validate parts of myimplementation that I am most concerned with. I don't do that sort of thing nowwith Web Forms don't even try but if it is an efficient way to help me delivermore robust applications, I'm so on it.
One of the few things that bothers me about MVC isthat its style of development hearkens back to classic ASP (and, for thatmatter, PHP) in that a typical page has a mix of code and HTML interspersed inthis messy mix of context switches. But MVC has a much better SoC approach(whereas ASP had none) that encourages clean, logical code. Time will tell if Ican let go of that slimy feeling I get with ASP when I write MVC apps.
Over time, Microsoft is adding features that willhelp reduce the amount of code you need to write for an MVC applicationcompared to Web Forms. (For example, templated helpers, which let youautomatically associate edit and display elements with data types, looksintriguing.) But only time will tell whether that becomes too much like thegooey mess I have to deal with in Web Forms with the postbacks, the Page objectand events, and other things I have to fight against far too much. I dearlyhope that MVC will remain lean and mean while offering productivityenhancements for developers.
The impending release of the second version of MVCwill only make it stronger and a more viable alternative for creatingASP.NET-based web applications. Of course, there are a lot of enhancementscoming in ASP.NET 4.0 that will make Web Forms better and ease some of itsproblems. You can find a summary of what's coming in MVC 2.0 on the ASP.NETMVC Roadmap page on Codeplex.
The major component vendors are starting to releaseproducts with support for ASP.NET MVC, so all your favorite widgets are likelyto be available. That will make developing MVC applications even easier.
Here are the two best analyses of Web Forms versusMVC that I've found:
Rick Stahl's blog entry, "What's Ailing ASP.NETWeb Forms." This postdates back to 2007 when MVC was just announced byMicrosoft, but still has some of the most insightful analysis of thedifferences in the two technologies. And although it strays somewhat from thetopic, it led to some very interesting comment discussions.
Dino Esposito's "ComparingWeb Forms and ASP.NET MVC article" in the July 2009 issue of MSDN Magazine. This article provides aconcise summary of the differences between the two technologies, long after thefirst release of MVC. More than most discussions you'll find, it gives abalanced view that should support your decision to go with either technology.
There are plenty of other analyses and discussionson the topic around the web, which a Google or Bing search will readily find.
In the end, use the version of ASP.NET that is bestfor you and your application. Web Forms are decidedly not dead and are asviable as ever. Neither technology is going away anytime soon, and Microsofthas committed to aggressively improve both. But for me, right now, right here,MVC is proving to be an almost irresistibly compelling because I don't seem tohave to fight with it so much to do what I need. And that is compelling indeed.
Don Kiely ([email protected]), MVP, MCSD, is a senior technologyconsultant, building custom applications and providing business and technology consulting services. His development work involves tools such as SQL Server, Visual Basic, C#, ASP.NET, and Microsoft Office.
About the Author
You May Also Like