Plip's Weblog

Phil Winstanley - British .NET chap based in Lancashire. Enjoys tea and tech. Working for Microsoft.

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: , ,

Comments

Ian Smith said:

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.

# December 9, 2007 9:43 AM

rrobbins said:

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.

# December 9, 2007 10:40 AM

Dee said:

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.

# December 9, 2007 10:49 AM

Chris Hardy said:

MVC is right for me, thanks Phil.

# December 9, 2007 11:44 AM

foobar said:

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.

# December 9, 2007 12:43 PM

rekna said:

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).

# December 9, 2007 1:14 PM

The Other Steve said:

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.

# December 9, 2007 1:44 PM

Gauthier Segay said:

"""

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 ;)

# December 9, 2007 5:38 PM

foobar said:

> 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.

# December 9, 2007 10:20 PM

Nikhil Kothari said:

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

# December 10, 2007 2:36 AM

James Knowles said:

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.

# December 10, 2007 8:09 AM

Niki said:

"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.

# December 10, 2007 5:34 PM

Gernot Kogler said:

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.

# December 11, 2007 2:57 AM

llj098 said:

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

# December 11, 2007 11:03 AM

Matt said:

---

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.

# December 11, 2007 11:36 AM

tapper said:

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.

# December 11, 2007 6:32 PM

Rich Costall said:

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

# December 13, 2007 7:20 AM

Craig said:

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.

# February 3, 2008 5:53 PM

Kevmeister said:

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.

# February 6, 2008 12:40 AM

shiju said:

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

# April 3, 2008 10:05 AM

Sgro said:

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

# April 23, 2008 9:53 AM

Derek Greer said:

"It [Web forms] works with both your level of knowledge and your approach to building Web Applications ... 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."

Having first developed Unix C-CGI Web applications for quite a number of years and then Java applications for several years after that, I adapted to ASP.Net fairly quickly because it was just a layer of abstraction over what I had already been doing at a lower level.  It was liberating in some respects, as it generated code I had to do by hand in the past, but it was also constraining at times as well.  It seems that moving from Web forms to MVC will remove some of the abstraction (and constraints) that Web forms comes with, so I can see where this may be problematic for those who never quite learned what what going on under the covers. I think that people with a grasp of the fundamentals of Web development may actually have a far easier time working with the MVC framework than with Web forms, notwithstanding the present (and temporary) lack of designer support.

# December 14, 2008 12:17 PM

zakizakaria69 said:

I strongly agree with Phil. The followings are my reasons for not using MVC Framework:

1. Long learning curve. I am 41 now and it gonna takes me 2 to 3 years to master this framework (if strictly follow Microsoft MVC rule). If MVC is about separation of concern...then I have done that using the exiting n-tier provider pattern.

2. What about the existing rich Ajax control from 3rd party companies like Telerik and ComponentOne and from others. So functional and useful. If Microsoft could give better ways to use the these existing components into their new MVC paradigm, then I could consider of adapting this MVC.

3. MVC has been long in Java community but the they are now shifting to .Net because many businesses were and are now impressed with .Net solutions that are fast to be developed and very rich under simple programming model. It takes me 6 months to decide which programming language and platform for to use many aspects of enterprise level of programming without so many hassles. 4 years ago I was a windows programming using VB and Java. I am analyze PHP, JSP, VB.Net, C#, PERL and have tried progamming these language with Linux and Microsoft platform. Finally, I choosed ASP.Net and C#. As me come from corporate community learning programming by myself, time-to-market is the key. With MVC, I gonna busy get my code maitenanable and then when new requirements come, I will have real big problem.

4. Testing can be done in many ways and it is up to us to strategize testing and solution quality (not only code quality). I usually comfortable testing my code in phases and at component level. Component-based programming really reduce my errors. It's already maintainable.

5. .Net is beautiful because we could achieve many things in many ways depending on the level of understanding. Performance is influenced by many characteristics; bandwith, code quality, database, hardware, etc. Viewstate issue is very minimal. There many ways to solve the "state" problems.

6. MVC is a concept. Maybe in the future, we could come up with a better and simple way to achieve MVC concept using the existing components and programming model. It doesnt have to be perfect. We actually were really near to MVC concept long ago until Microsoft reponded to MVC request from Java community with MVC Framework.

Phil, thanks for your post. You are the real champion. Liberty is the key.

# February 22, 2010 10:09 PM

suthahar said:

Hi I have 3 years experience in asp.net now i will move MVC ,its easy for me

# April 21, 2010 4:00 AM

mshsayem said:

MVC is just awesome, in a single word.

# July 16, 2010 4:28 PM

Prophe'C said:

The devise of MVC appears rather targeted for those revenge of the nerds type Software Developers who migrated over to web development. MVC essentially is that of n-tiered development that software use... Nothing truly wrong with web forms, CSS/HTML5 & Javascript or AJAZ.NET and ADO.NET... nothing.

But it must take something new to keep the world spinning around MSFT it seems.

SPARQL anyone?

# August 11, 2010 9:56 PM

Pradeep said:

Hi Phil,

Nice article, to get an idea on ASP.NET

You said "MVC is not Perfect". In which context I can take this statement? Is it mean that, ASP.NET MVC is not well proven tachnology?

I think MVC may have below advantages over Web Forms.

1. Viewstate, which I don't like.

2. MVC gives full control on rendered HTML.

3. On top of all, MVC brings a kind of structured / consistant programming model among the large team.

I think ASP.NET MVC is good for large projects, mainly when consider maintainability in long run.

Pradeep Y.

# September 22, 2010 6:05 AM

ipad app pages said:

you are a slow walker,but I never walk backwards.

-----------------------------------

# December 21, 2010 8:12 PM

Felipe Arterburn said:

Hi, I can't figure out how to add your website in my RSS feed reader. Can you tell me what I'm doing wrong, please.

# July 5, 2011 9:04 AM