Is MVC right for you?

With the imminent release of the ASP.NET MVC Framework, I thought I'd explore some of the arguments behind using it.

This post will invariably be contentious and cause a ripple of indignation amongst the proponents of the MVC approach but I feel it's important to explore the arguments behind adoption of the new model as well as the technical and emotional implications of moving to the new development process.

One thing that has irritated me about the discussions which have spawned from the publication of details around the MVC framework is the smear campaign which has been started around the Web Forms model. I strongly support the Web Forms model and believe that the majority of the flaws in Web Forms that MVC purports to overcome are easily solvable by following the guidance and advice of experienced ASP.NET developers.

I want to bring the argument out of academia which is where it's been relishing in it's position for some weeks and back into the real world of actual day to day development.

So to help explore some of the propaganda issues raised by the MVC crowd we'll look at each of the arguments in depth (how deep will be determined by the appropriate level of rage it incites in me).

 

MVC is to Web Forms as Incarceration is to Liberty

It's true, MVC is not the model for people who want the freedom to be creative with their code. It's the pattern you follow if you want to conform with a particular doctrine of programming. There is (if you follow it to the letter of the law) not a huge degree of flexibility.

This is just the nature of the beast, it's designed in a way which not only encourages you, but in many cases, forces you to program in a certain way. Now, that can be a good thing, some of the ASP.NET code I've seen in my time has literally made me cry it's been so badly constructed.

With Web Forms as a developer, you're pretty much free to follow the path which suits you best. It works with both your level of knowledge and your approach to building Web Applications and allows you to do what's best for you. It helps you achieve your end goal by not obstructing you along the way.

lib·er·ty

–noun, plural -ties.

freedom from control, interference, obligation, restriction, hampering conditions, etc.; power or right of doing, thinking, speaking, etc., according to choice.

MVC forces these patterns on to you for good reasons, if there's only one way of doing things, there's only one thing to learn and only one set of advice to follow. No confusion, no misunderstanding, anyone can pick up the code you write and should be able to instantly understand it if they understand MVC. That's the theory anyway. In reality, you as a developer can still put code in the wrong place, you can still make bad decisions on how to structure your application and you can still essentially cock the whole thing up.

in·car·cer·ate 

–verb (used with object)

to imprison; confine; enclose; constrict closely.

The argument that you cannot achieve a level of maintainability and testability with Web Forms is not only misplaced, I believe it's fundamentally wrong.

If your Web Forms applications are written in a well structured way, where you separate out your Business Logic from your Presentation and where you're clearly defining the so called boundaries around tiers, then your application is in my mind as maintainabl4e as one following the MVC model to the letter, with one very important difference. You can take advantage of the full control framework with Web Forms which to date just doesn't exist with MVC. The time saving of control is I believe of fundamental importance.

"If you want a model that pretty much *forces you* to write your application in a way that is testable, maintainable, and pluggable, MVC is great."

It's very much an argument which closely follows the views of the two main political parties here in the UK. I'm sure that's just a coincidence though...

Here's what the Conservative Party say: -

Giving people more opportunity and power over their lives

In our open and dynamic world people's horizons are broader: their ambitions are greater, and they expect to be able to make more and more decisions for themselves. Government, politics and public services need to catch up.

Here's the approach the Labour party takes: -

The Labour Party is a democratic socialist party. It believes that by the strength of our common endeavour we achieve more than we achieve alone, so as to create for each of us the means to realise our true potential and for all of us a community in which power, wealth and opportunity are in the hands of the many, not the few.

Does that mean Web Forms is a more Conservative approach compared to the Socialist approach of MVC? Possibly. For reference, I'm neither a Conservative or a Socialist, I'm a Liberal.

I do however digress, let's return to the real world aspects of this.

 

MVC makes testing your applications possible

 

I'd agree testing is hard with Web Forms. In fact, I found it so difficult I abandoned trying to unit test my ASP.NET code and moved to a process of using Watir and testing the rendered HTML instead.

 

Now, I don't know how many .NET developers (and I'll include all .NET developers here, not just us superior Web types) perform unit testing but if I were to take a conservative guess, I'd probably put it at less than 1% of all .NET developers who unit test (I'd love it if I were wrong - but I fear I might even be being a little adventurous with those figures).

 

If people adopt MVC and consequently begin testing their web applications, MVC has done more for ASP.NET than four or five years of Test proponents like Andrew Stopford banging on about MbUnit has done; that can only be a good thing. However, if people adopt MVC and then consequently do not begin Unit testing their code, what's the point? Are the so called Maintenance and HTML/URL control benefits worth loosing all the Web Forms goodness? It's an interest point, one to which I do not know the answer.

 

Applications written using MVC will be more maintainable

 

Can't see the Code for the Patterns. Every developer has a way that they write code. A signature if you will, where you can pick up code and say - "This look's like Bob's work".

"It isn't impossible to achieve maintainability and separation with WebForms but it doesn't make life easy and leaves you to roll something yourself or follow a Patterns & Practices recommendation."

The quote above is from an internal Microsoft discussion list. I think it's very true. Those of us who have been building ASP.NET professionally for years are now in a position where it is easy. We have our pattern to follow and we know how to build good maintainable ASP.NET applications.

Structure and form becomes an integral part of our Web Forms applications.

If we're already following those methodologies, does MVC offer us anything more? One thing it does offer is conformity. Everyone will create MVC applications in the same way (I hope).

 

Learning curve

 

Teaching people ASP.NET isn't an easy thing to do because of the sheer size and architecture. There's the .NET Framework, ASP.NET on top of that, the Languages you might use to program against .NET and ASP.NET and then of course there's the implementation of the platform you're using. It's a huge amount to learn.

Luckily, one thing I've found with ASP.NET Web Forms is that you can teach people a small amount at a time and they can over a period of time incrementally build their knowledge of the platform. It's quite a smooth learning curve in my opinion.

 

What does this smooth curve mean? Well, for a start, people generally aren't that afraid of having a go, the concepts being shown to them aren't overwhelming (once you've convinced them to use C# and not VB.NET ;) ). The smoothness of the curve means people can learn just what they need to learn to do the job.

With the mass of technical articles, blog posts and snippets out there around ASP.NET Web Forms one other great advantage is that almost any problem you can face with ASP.NET will be documented and the solutions listed somewhere on the Web, it's just a matter of searching for them.

"Programmers more than nearly any other demographic, despise change. Programmers not only despise change, they run from it and tell other people that change will kill them as they trample innocent bystanders."

MVC, I fear, won't have this same curve.

If you're used to ASP.NET Web Forms and move to the MVC approach, so much of the knowledge you've gained (lovingly) over the years of ASP.NET development is effectively lost. You have to start again. Whilst the language, platform and the IDE are the same, that's pretty much it. The controls you use today aren't there, the way that you structure the events that happen in your pages no longer work that way, pretty much everything is different. It's a complete shift in the way you'll build your applications.

MVC needs you to learn everything in one go, whereas Web Forms allows you to incrementally build up your knowledge.

 

Inline Code (shudder) and control over your HTML and JavaScript with MVC

I've spoken with ASP.NET authors, trainers and speakers over the past few weeks and months about MVC and one thing comes up again and again in conversation. We as a community have spent years persuading developer that inline code is not only bad, but in fact bordering on evil. The mess that Classic ASP pages could get into because of nothing more than inline code was not only shockingly prevalent within the Classic ASP community but also was a massive overhead on maintaining applications.

One thing the MVC guys talk about is the separation of concerns which refers to the way in which you have the "model" and the "view" and the "controller" (MVC!); whilst they bang on about this, the separation of markup from code is completely disregarded as a non issue.

There are a set of MVCToolkit HTML Helpers which make life easier when constructing what we'd recognise as controls but again, it's for some, a shift in thinking again.

Inline code makes me very wary, whenever I see someone sneaking it into an ASP.NET page they get a good bollocking about it.

 

In Summary

I fully support the creation of the MVC framework and the ASP.NET implementation of it. I believe for a small minority of ASP.NET developers, it will being them huge benefits. I will personally be using and evaluating the MVC Framework over the coming weeks and months to continue to expand my knowledge of the processes and techniques required to build well formed MVC applications.

I'd encourage all ASP.NET developers to follow the posts Scott has been putting up about the MVC framework and give it a shot. It's got many advantages as we've gone through here. There's a phrase I've heard a few times, I believe it has an American origin. It seems to be quite apt when talking about the MVC Framework.

 

"Your Mileage may vary"

 

So, to summarise: -

  • It's important that people understand Web Forms are not being abandoned by Microsoft.
  • MVC is not perfect.
  • MVC is not easy (if you're a Web Forms person already).
  • You will have a choice moving forward of using Web Forms or MVC with you applications.

 

Please, if you're going to flame me, let me know first and I'll turn off the central heating.

Ta,

Phil.

 

Technorati Tags: , ,
Published Sunday, December 09, 2007 1:35 PM by Plip

Comments

# re: Is MVC right for you?

Sunday, December 09, 2007 9:43 AM by Ian Smith

Great post Phil! Refreshing to read something that doesn't just read like "I drank all the Kool-Aid" on something that is being blogged about everwhere when it hasn't even been made available yet.

# re: Is MVC right for you?

Sunday, December 09, 2007 10:40 AM by rrobbins

I already struggle to get ASP.NET web controls to do something that would have easy to do with classic ASP where I have complete control over the HTML that is generated.

# re: Is MVC right for you?

Sunday, December 09, 2007 10:49 AM by Dee

Common structure and rules are a good thing.  Like our national transportation system, it's good to have the same "rules of the road" regardless of where you are traveling.

# re: Is MVC right for you?

Sunday, December 09, 2007 11:44 AM by Chris Hardy

MVC is right for me, thanks Phil.

# re: Is MVC right for you?

Sunday, December 09, 2007 12:43 PM by foobar

rrobins:  Then don't use the ASP.NET web controls.  There's absolutely NOTHING that prevents you from using standard HTML in WebForms, other than your inability to realize that you can.

Anyhoo, I've created MVC web apps for years now (not in ASP.NET).

Wait till we see the posts about monolithic controllers, people putting ALL their logic into the controller, whether passive views versus active views are best, yadda yadda...

Every form of design has positives and negatives.  The people who espouse MVC as the panacea to WebForms...  Well, I have to wonder just how much MVC experience they truly have.

# re: Is MVC right for you?

Sunday, December 09, 2007 1:14 PM by rekna

A web framework should have following goals:

- productive & efficient programming

- reuse of components

- maintainability

- testability

What Microsoft is lacking:

- to be productive, a real Ajax framework (integrated in the controls, not on top of it, extender model is too complicated and not intuitive, updatepanel does not go far enough, not real ajax). I would like to see usercontrols on a page, exchanging data (in both directions) without having to post whole webform.

- abstraction from HTML,CSS,javascript for page layout : there are too many cross browser issues . An productive .NET developper should not have to deal with these.

- an object persistence framework for a business layer and good support for it in the ASP.NET framework

- integrated unit testing of business layer (I agree that UI testing is not easy to do, and not many developpers do it or don't have the time for it).

# re: Is MVC right for you?

Sunday, December 09, 2007 1:44 PM by The Other Steve

Thank you for this.

One of the problems I've had with reading some of the MVC proponents articles is that they keep talking about how bad Viewstate is, how bad postback is for state management and so on.

Well yes, it usually is bad if you are building a large scale application.  But it's helpful for something small scale and quick to get it up and running.

But most importantly, you don't have to use viewstate or postback.  For years I've been building asp.net applications which don't use much in the way of viewstate.  If I have a datagrid and I want to sort it, I set the link to call back to the existing page with a sort parameter on the querystring.  I prefer to do things this way, so that a user can favorite the page as sorted.  Same with navigation.  I use real links, not postback buttons.  Click here and you go off to a new page with the item selected, etc.  I only use a postback, if I'm submitting some data from a form.

But I think the fundamental point is, I can already do this with the existing framework.  I don't have to have MVC to go down this path.

I just get the feeling that everybody is talking past one another.

# re: Is MVC right for you?

Sunday, December 09, 2007 5:38 PM by Gauthier Segay

"""

If you're used to ASP.NET Web Forms and move to the MVC approach, so much of the knowledge you've gained (lovingly) over the years of ASP.NET development is effectively lost.

"""

If started learning the basics of the web instead of "point-click/designers/wizards/flaky code behind mixed with business logic", the learning curve for ASP.NET webforms is actually steeper than plain MVC as found in RoR or Monorail.

IMO the "power" of the one single server form is overlooked, and tossing it away will give us back control (and readability) on what occurs on HTTP requests (one method to look at in the controller) and what we sent back on HTTP responses.

All this debate makes me wondering why don't I've a blog to rant along ;)

# re: Is MVC right for you?

Sunday, December 09, 2007 10:20 PM by foobar

> If started learning the basics of the web instead of "point-click/designers/wizards/flaky code behind mixed with business logic", the learning curve for ASP.NET webforms is actually steeper than plain MVC as found in RoR or Monorail.

Why do people keep saying this?!?  This is patently untrue.  Jeez, before WebForms came along, all there was were "the basics of the web".  

Really, if you can't figure out WebForms, don't bother programming.  You won't stand a chance when you actually need to do something beyond the trivial CRUD apps everybody else besides me  seems to be building.

# Questions about ASP.NET MVC

Sunday, December 09, 2007 11:20 PM by More Wally - Wallace B. McClure

My buddy Plip posted his thoughts on ASP.NET MVC today. Its been swirling around in my head all day.

# re: Is MVC right for you?

Monday, December 10, 2007 2:36 AM by Nikhil Kothari

Great post, Plip! Super interesting, and well put.

# Is MVC right for you?

Monday, December 10, 2007 3:05 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Is MVC right for you?

Monday, December 10, 2007 8:09 AM by James Knowles

Phil, great article. Any devs who really pick one way to do development, such as saying that MVC is the only way to go will, find themselves one day banging a square into a round hole. It has a place, I am quite interested to see the effect has on SEO environment.

# re: Is MVC right for you?

Monday, December 10, 2007 5:34 PM by Niki

"But most importantly, you don't have to use viewstate or postback.  For years I've been building asp.net applications which don't use much in the way of viewstate.  If I have a datagrid and I want to sort it, I set the link to call back to the existing page with a sort parameter on the querystring."

Yes, of course you can do this, but if you take the "path of least resistance", you get ViewState/postback-State-Management with "classic" ASP.NET. You _can_ propagate state via Query-Strings, but you have to jump through lots of hoops, while implementing a paging/sorting Grid with a connected details-View requires virtually no code.

That said, I think the main problem with the current MVC implementation is that it's more or less mutually exclusive with classic server controls, mainly because postback doesn't really work. I'm hoping they fix this in a future version, then we could use MVC for the high-level application structure, and server-controls/postback for the tiny little view-details.

# re: Is MVC right for you?

Tuesday, December 11, 2007 2:57 AM by Gernot Kogler

First: Your Blog doesn't render right in Safari and Firefox. Please, MS devs, stop making proprietary web sites, there's life beyond IE ;-)

Concerning MVC: ASP.NET ist just a bad programming model for the web. It was an attempt to get fat client programmers to the web. I am one of them, but I never liked the idea that a web page is something like a winform, because it isn't. Then I tried Ruby On Rails and suddenly everything was easy and just "fitted". MVC is the right way to go and finally MS found out, too.

# links for 2007-12-11 | Lazycoder

Tuesday, December 11, 2007 5:25 AM by links for 2007-12-11 | Lazycoder

Pingback from  links for 2007-12-11 | Lazycoder

# re: Is MVC right for you?

Tuesday, December 11, 2007 11:03 AM by llj098

i use FF too,i have no problem on this page~~

# re: Is MVC right for you?

Tuesday, December 11, 2007 11:36 AM by Matt

---

One thing the MVC guys talk about is the separation of concerns which refers to the way in which you have the "model" and the "view" and the "controller" (MVC!); whilst they bang on about this, the separation of markup from code is completely disregarded as a non issue.

---

Certainly don't agree with you on that.  I've been doing MVC work with Struts and JSF for quite a while now.  I don't know ANY experienced Java developers who think it's OK to use scriptlet code in their JSPs.  You can certainly do it if you want but it's generally accepted that this is very poor form.

# re: Is MVC right for you?

Tuesday, December 11, 2007 6:32 PM by tapper

Gernot Kogler,

So stick with RoR then. But you know what? for all the whingeing from the RoR crowd I can't think of any major project that uses RoR. As for ASP.NET masquerading as winforms, that was the whole point. MVC has its place but as plip said, if you've already been doing ASP.NET with a decent architecture MVC isn't going to bring a hell of a lot to the table.

# re: Is MVC right for you?

Wednesday, December 12, 2007 3:25 PM by Shane

Interesting article Phil; gotta say, I came across Scott's article about MVC in ASP.NET the other day, and was quite surprised.  

It's an interesting alternative to the traditional ASP.NET model, but it is just an alternative.  I'd be as pissed off as anyone if it was replacing the existing framework, but it'll apparently sit alongside.  

Although RoR isn't the only framework that uses MVC, I'm sure that its recent popularity has something to do with the adoption of the pattern in ASP.NET.

It's an interesting inclusion, but for ASP.NET, I think I'll be sticking to what I know.  As for anybody else using it, hey - whatever does it for you :)

# re: Is MVC right for you?

Thursday, December 13, 2007 7:20 AM by Rich Costall

Well put Phil, I'm not the only one then :-). It was the inline stuff that really scared me...

What it does provide though, is a framework which those ruby type and python typep people  maybe be more familiar with, as an alternative, but I know where i'm stopping...

Rich

# Our daily link (2007-12-12)

Sunday, December 16, 2007 1:32 PM by Trumpi's blog

Software Parallels CTP - please don't all rush at once to get this. Otherwise we'll have a race

# Is WebForms Right for You?

Wednesday, January 30, 2008 1:10 PM by Insert Catchy Title Here

Is WebForms Right for You?

# re: Is MVC right for you?

Wednesday, January 30, 2008 1:30 PM by Bill Pierce

Allow me to retort.

blechie.com/.../Is-WebForms-Right-for-You.aspx

# My Dad Can Beat Up Your Dad

Wednesday, January 30, 2008 2:26 PM by Mike Nichols - Son Of Nun Technology

I was waiting for the MVC versus WebForms smack downs to begin :) Who wins my personal choice has been

# re: Is MVC right for you?

Wednesday, January 30, 2008 3:49 PM by Ben Scheirman

A couple notes:

your view examples *are* ugly and there are plenty of ways to slim it down.  My rule-of-thumb:  If your view isn't immediately readable, then you're doing something wrong.

"Are the so called Maintenance and HTML/URL control benefits worth loosing all the Web Forms goodness?"

if you call Web Forms goodness the nasty non-standard HTML crap that a handful of webforms controls render, then yes.  If you call the static URL's tied to filename's goodness, then yes.  If you mean having a monolithic object that has properties and events on it that only apply in certain scenarios with viewstate turned on (I'm talking to YOU gridview) then HELL YES.

Seriously, you seem like a solid web forms developer, given all of your points I'm surprised to see this tone coming from you.

# re: Is MVC right for you?

Friday, February 01, 2008 7:42 PM by Steve

What a load of crap.  You've been brainwashed.

This Webform setup has been a mess from day one, created for those that have never done web development.

I know, I've been working on it literally since day 1

Liberty?  More like 'I've learned this crappy MS setup, now I'm incapable of learning anything else - I must be encapsulated away from post and get, I like having complex page lifecycles..."  You call that freedom?

# What could Microsoft's and Yahoo's marriage mean for developers?

Saturday, February 02, 2008 6:25 PM by Plip's Weblog

For many the marriage of this rather unlikely coupling is both disturbing and terrifying, but I actually

# re: Is MVC right for you?

Sunday, February 03, 2008 5:53 PM by Craig

WebForms was invented as an easy way to get Windows developers on the web how couldn't understand the basics like GET/POST. It is not much more than a hack. It must say something that anyone who develops in another web tool, ie RoR, PHP etc then .NET feels totally foreign to them, because it breaks the web metaphor. All of a sudden with the MS MVC it feels normal again.

# re: Is MVC right for you?

Wednesday, February 06, 2008 12:40 AM by Kevmeister

MVC represents a breath of fresh air for me. Microsoft seems to like taking one paradigm and trying to pretend the same paradigm exists in another realm, like making WebForms look and behave like an event-driven WinForms app, with postbacks used to shuttle the events around.

For a simple app this is fine, but it quickly outlives its suitability to the task, and dies even quicker when a high-latency WAN is involved. The user experience simply degrades too quickly when trying to do too much event-driven work posting back to the server. And complex pages continually regenerating themselves is simply inefficient use of resources.

So in my mind another methodology had to be forthcoming, one that takes you back closer to "the metal" and lets you mix-and-match different techniques where required to achieve the right outcome.

MVC is a good foundation to build on.

Problem is we're now very used to the "minimal" effort needed to build these WebForms apps (whilst tending to overlook their deficiencies) and keep trying to convince ourselves that it's good enough for the job, even though it really ain't that good, compared to what's possible on a clean slate.

Microsoft's major deficiency with MVC is not [seemingly] having an alternative set of controls that can be used for typical data scenarios, such as DetailView and an alternative GridView. Stuff that has been built with AJAX at its core where it makes sense, not relying on an external AJAX wrapper like UpdatePanel. Controls that provide the absolutely best way to build a high-performance and responsive web app. And because MVC means that a controller is distinct from a view, routing AJAX requests for additional data now become more self-consistent with the page requests themselves.

# re: Is MVC right for you?

Thursday, April 03, 2008 10:05 AM by shiju

ASP.net webform is an outdated model and ASP.net MVC will be a clear winner

# re: Is MVC right for you?

Thursday, April 10, 2008 1:20 PM by Bunter

I've been developing ASP.NET WebForms from day one and the benefits from convenient viewstate are really not worth the pain webforms cause. Have a page with listing of some more complex objects and datagrid or repeater. Oops, your viewstate just grew to 800 KB. Wanna more interlinked application. Oops, you have to start grabbing parameters from querystring and viewstate. Wanna separate application and UI code. Oops, you have code-behind giving you a big helping hand messing everything together. Wanna test something at the application level? Oops, webforms code-behind is basically not testable. Want to have something called application, separated from UI? Oops, webcontrols and pages (basically views) are central driving force for your application not your application driving the views. Wanna build additional layers (security, logging, whatever)  easily and noninvasively. Oops, not really easy with webforms (compare this to MonoRail with method interceptors, replaceable service stack).

It is not that you cannot have decent architecture with webforms. It is that you have to work much harder to achieve it. I've used MonoRail for few new projects and really complex problems have found really simple solutions. With webforms when it comes to application architecture, simple problems find complex solutions.

And this "It's true, MVC is not the model for people who want the freedom to be creative with their code." part made me laugh my pants off. Compare the monolith framework like webforms and mvc implementation like MonoRail or upcoming MS MVC where everything is pluggable and replacable and conclude, that webforms give you a lot of freedom? Does not compute.

# re: Is MVC right for you?

Wednesday, April 23, 2008 9:53 AM by Sgro

There's one thing you didn't mention in your article, and is so important that is a shame that you didn't talk about it.

MVC gives you total control over the output. No more non-standard html, no more "style" attribute rendered even if I don't want it. No more stupid page-life-cycle that's only confusing and too far away from the concept of HTTP requests. And no more stupid self-only postbacks.

And there's more to it. Many many stuff in web forms only works with javascript enabled, and one of the first rules to a good web site (or application) is that it should work in EVERY main function even with JS disabled (progressive enhancment or graceful degratation, you choose).

And there's still more. No more viewstate (hell it made pages a lot heavier), no more form runat=server and I could continue.. but I think I gave the idea.

MVC framework is what every serious web developer was waiting for: standard HTTP requests, standard (X)HTML, total control with the power of C#, masterpages and all the nice stuff in the .NET framework.

In one word: AWESOME

Leave a Comment

(required) 
(required) 
(optional)
(required)