29 Dec 2008

Introducing the ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms

16 Comments Uncategorized

This is a continuation of my Introduction to ASP.NET MVC series.  As I outlined before this is in an effort to write the book and keep blogging, I decided to write/blog the last chapter, Chapter 2.  I am doing this so I can receive feedback on this chapter as early as possible.  Because this chapter, in my opinion, is probably the most critical of the book, it defines the context around ASP.NET MVC and how it differs from ASP.NET Web Forms, as well as giving a historical perspective of the MVC pattern.

In the next several posts we will cover the following parts of Chapter 2 from the book:

by Nick Berardi

New: $31.49
This item has not yet been released. You may order it now and we will ship it to you when it arrives.

ASP.NET MVC vs. ASP.NET Web Forms

As you have seen, in the previous section, and can probably imagine MVC is going to be an architectural pattern that is going to be around for the foreseeable future, especially on the web.  So it is very important to internalize and understand the major differences between ASP.NET MVC and the older ASP.NET Web Forms.

ASP.NET Web Forms

Starting with the .NET Framework Version 1.0, in January 2002, Web Forms was Microsoft’s first real attempt to provide a first class web application layer that was both robust and flexible enough to meet the demands of the web at that time.  ASP.NET Web Forms has proven to be a mature technology that runs small and large scale websites alike.  Web Forms, was built around the Windows Form construction, where you had a declarative syntax with an event driven model.  This allowed visual designers to take full advantage of the drag and drop, WYSIWYG, interface that they had become accustom to under Windows Forms development in Visual Studio 6.0.  In that they only needed to drop controls onto the ASP.NET page and then wire up the events, as was common in Visual Basic 6.0 development at the time. This made Web Forms a natural choice for Windows Forms developers, because the learning curve was low and the need to understand HTML and many of the web centric technologies almost zero.  Web Forms have many strengths and weaknesses:

Strengths
  • Mature technology
  • Provides very good RAD development capabilities
  • Great WYSIWYG designer support in Visual Studio
  • Easy state management
  • Rich control libraries from Microsoft and third party vendors
  • Abstracts the need to understand HTTP, HTML, CSS, and in some cases JavaScript
  • ViewState and PostBack model
  • A familiar feel to Windows Forms development

Web Forms has grown so much since 2002 because it has the ability to do great things that are often much harder to accomplish in other frameworks.

Weaknesses
  • Display logic coupled with code, through code-behind files
  • Harder to unit test application logic, because of the coupled code-behind files
  • ViewState and PostBack model
  • State management of controls leads to very large and often unnecessary page sizes

Web Forms is not all roses and buttercups, there are some serious setbacks that usually show up when you are trying to optimize your code for scalability, the biggest problems are the ViewState and PostBack model.  ViewState is a way to store the state of the controls, such as data, selections, etc, which is needed to preserve the Windows Form like development habits of the developers.  ViewState was necessary, because the web is a stateless environment meaning that when a request comes in to the server it has no recollection of the previous request.  So in order to give state to a stateless environment you need to communicate the previous state back to the server, in Web Forms this was accomplished using hidden <input /> fields that can become ridiculously large. This increased size becomes apparent when server controls such as GridView are added to the page.  PostBack was another creation to facilitate the Windows Form development feel, it renders JavaScript for every subscribed event, which leaves web developer less control over how the browser communicates with the server.

ASP.NET MVC

ASP.NET was often overlooked as a viable platform for modern highly interactive websites that required a very granular control over the output of the HTML, because of the lack of control over the rendered HTML.  This granularity of control was sacrificed in Web Forms to make if more like Windows Forms development, in other words easier for the drag and drop developers.  This lack of control over the HTML rendering forced developers to move the platforms such as PHP and Ruby on Rails, which offered the level of control they required and the MVC programming model that provided a necessary separation of concerns for their highly complex web applications.

This led Microsoft to announce in the Fall of 2007 that they were going to create a platform based off of the core of ASP.NET that would compete against these other popular MVC web centric platforms.  Microsoft implemented ASP.NET MVC to be a modern web development platform that gives a ‘closer to the metal’ experience to the developers that program with it, by providing full control and testability over the output that is returned to the browser.  This is the main and most important different between Web Forms and MVC, in my opinion.  MVC has many strengths and weaknesses

Strengths
  • Provides fine control over rendered HTML
  • Cleaner generation of HTML (well as clean as you keep it)
  • Clear separation of concerns
  • Provides application layer unit testing
  • Can support multiple view engines, such as Brail, NHaml, NVelocity, XSLT, etc.
  • Easy integration with JavaScript frameworks like jQuery or Yahoo UI frameworks
  • Ability to map URLs logically and dynamically, depending on your use
  • RESTful interfaces are used by default (this helps out with SEO)
  • No ViewState and PostBack model
  • Supports all the core ASP.NET features, such as authentication, caching, membership, etc.
  • Size of the pages generated typically much smaller because of the lack of the ViewState
Weaknesses
  • Not event driven by the framework, so it maybe more difficult for ASP.NET Web Form developers to understand
  • Requires the need to understand, at least at the basic level, HTTP, HTML, CSS, and JavaScript
  • Third party library support is not as strong
  • No direct upgrade path from Web Forms
  • No ViewState and PostBack model (makes it more difficult to preserve state)

As you can see the pros and cons of MVC have to be weighed just as much as Web Forms, and MVC is not always the logical choice.

How do I choose?

It’s up to you to decide and you choice needs to be weighted with a number of other factors, such as team and application requirements, when deciding which ASP.NET technology to implement.  I have developed the following worksheet that will hopefully help you decide, when you need to make this decision.

The Worksheet

This worksheet is meant to be a guide for when you are trying to choose between Web Forms and MVC.  The values in the Value column are representations of how easy it would be to accomplish the requirement in either MVC or Web Forms.

The values range from 0 to 30, zero being no effort to implement and 30 requiring a great amount of effort to implement.  If the value is positive then it is an advantage to MVC, if the value is negative then it is an advantage to Web Forms.  For example if the value is, +4, then the requirement is more suited for MVC, and it would take a little work to get it to work as easily in Web Forms.  Alternatively, if the value is, -25, then the requirement is definitely suited for Web Forms, and it would take a lot of work to get the requirement to work with MVC.

Requirement Value Selection
Clean HTML Rendering

+4

Use RAD Designers

-5

Use TDD (Test Driven Design)

+8

Testability

+7

Data-heavy application

-10

Upgrade from Web Forms

-25

Need event-driven model compared to Windows Forms programming model

-7

Work on an Agile Team

+4

Need separation of concerns

+10

Creating a proof of concept or prototype

-6

SEO

+3

RESTful interface

+3

Need to preserve state of request

-2

Building an Internet Application

+3

Building an Intranet Application

-3

Need to support multiple views of the same application through mobile, web, and REST API

+7

Need to use existing third party controls for ASP.NET Web Forms

-10

Need control over the URL that is generated

+5

So after you make your selection and add up all the values, you should end up with a number representing your project.  Check that number against the table below, and you should have your answer as to which technology, MVC or Web Forms, is best for your project and team.

Value Reasoning

< -50

No Brainer, go with Web Forms

-25

Very Strongly Web Forms

-10

Strongly Web Forms

-3

Slightly Web Forms

0

It is a tossup between Web Forms and MVC

+3

Slightly MVC

+10

Strongly MVC

+25

Very Strongly MVC

> +50

No brainer, go with MVC

I want to mention one last thing if you have want to do new development in MVC, but you have to maintain some legacy Web Form pages, you can mix MVC and Web Forms in the same application. You just need to be aware of the differences between the two, and that you probably will not be able to share any of the Web Form front end code with MVC, such as Themes, Master Pages, and User Controls.

As you probably can guess by the name of this book, The Beer House application landed well in to the MVC side of the worksheet.  Which probably doesn’t surprise you if you are reading this book!

Feel free to use the above decision table and modify it for your own teams’ decision process.  The information provided above however should arm you with most of the important information that you need to know when deciding which way your project should go technology wise.

This post is licensed under a different license than the rest of my site. Copyright © Wiley Publishing Inc 2009

Tags: , , , , , ,
written by
Nick Berardi
subscribe
If you found this post valuable and would like to see more like it you can follow me.

16 Responses to “Introducing the ASP.NET MVC (Part 2) – ASP.NET MVC vs. ASP.NET Web Forms”

  1. Reply Lee Dumond says:

    Nick,

    Nice work so far. A couple of things I would point out:

    1. You mention “ViewState and PostBack model” as both a strength and a weakness of WebForms. This could be confusing to a reader. You clearly explain why it is a weakness, but not why it’s a strength.

    2. Right before the “How Do I Choose?” section, it looks like you cut off in mid-sentence there.

    3. Some of your worksheet criteria bear more explanation. Exactly *why* are WebForms more suited to data-heavy applications, for example? Maybe some of these criteria are explained elsewhere in the book, I don’t know.

  2. Reply Danny says:

    Nick ,

    Very clear and informative writing style. What’s the eta for your book ?
    Will there be an ebook version ?

    thanks

  3. Reply Nick Berardi says:

    Hi Danny, The ETA for the book is April 2009. I think there will be an ebook version, but it is up to the publisher.

    Lee, Thanks for catching that problem you mentioned in #2, I was actually moving stuff around, for flow, and missed that. Also I plan on addressing #1 and #3 on my second pass, tomorrow when I am fresh.

  4. Reply Eric says:

    I prefer ebooks to “dead tree” books. I’ve already signed up for a couple “preview” ASP.NET MVC books already. I’m not sold on ASP.NET MVC because I feel there is still a ton of mileage that you can get out of ASP.NET if you delve into custom server control creation.

  5. Reply Nick Berardi says:

    Eric, MVC isn’t suppose to replace Web Forms, after MVC is released there is going to be plenty of growth and work will not stop on ASP.NET Web Forms. That being said Microsoft is just creating an alternate way to develop off of ASP.NET platform. I predict that you will start to see a Internet/Intranet split, being that the majority of Internet developers will choose MVC and a majority Intranet developers will stick with Web Forms.

    Also as a side note when you say ASP.NET the terminology is going to be changing in the future, because both MVC and Web Forms will be running off of the core ASP.NET technology.

  6. Reply rajuyusuf says:

    Nice article. I found interesting post about how to handle ASP.NET MVC postback.

  7. Reply James says:

    One thing I see missing in all these comparisons is the business side – very few of the criteria mentioned actually reflect the business side of the decision making. What is needed is some type of comparison of the time / labor needed to produce equivalent apps in each framework. The business might want to look at some of the other intangibles, but from a business perspective, if the MVC approach drives the cost up 2x, then none of the ‘nice’ features of MVC may matter. Software costs are driven by labor costs – and it’s the single biggest decision point on selecting technology.

  8. Reply Samuel says:

    James, I’m totally agree with you for the money decision. I have tried to create a web site with MVC and each time I have to implemented a feature, it took me twice of the time. Ok, I’m new to MVC but if you want to do a nice use of MVC, you must follow rules and these rules; separation of concerns and the use of Model-View-Controller pattern require to create more code to make the same result.

    I love MVC for the power control over the html and the control over the Get and Post request but is those things worth the cost of many more hours of programmers $$$$…. In many projets, NO because the reality is that client want more in less time. In my business, we have projets like Elephant where MVC is very good but we have many projects like a rabbit where time is critical.

  9. Reply Bill Yeager says:

    From the tutorials I’ve started to do on the ASP.Net MVC site, there is, at least, so far, one glaring major weakness in MVC in my view.

    The Views are basically a mixture of HTML & C# pseudo code with no separation! This is a throwback to the classic ASP days when HTML was mixed with server side code. Talk about spaghetti code!

    With Viewstate compression getting more advanced, increase in bandwidth getting ever greater and HTML5 controls on the way for Webforms, the webform model for me is the cleanest separation of HTML & server side “real” C# code.

    ASP.NET 4.0 has added URL Routing, reduced ViewState, and greater control of the HTML mark-up produced by many ASP.NET controls.

    Another thing is the length of development time it takes to produce the same results. Developing in MVC takes way longer than WebForms.

  10. Reply lalita patil says:

    Thanks for another great post !!! MVC Model patterns are explained in a simple way.

Leave a Reply to Bill Yeager

Cancel Reply