ASP.Net Web Forms vs. ASP.Net MVC Models

Recently I attended a session on ASP.net 4.0 and VS 2010 to see what was coming down the pipe.  During this session the speaker discussed some of the areas of improvements in regard to ASP.Net MVC.  As he was doing his demo it reminded me of a question I had when I first heard of ASP.Net MVC, why would I as a ASP.Net developer even want or need to use MVC?  What are the benefits and how does it help me get the job done better and/or faster?  You see I have a long history in writing code,  I've been writing code for many years and have seem the likes of SmallTalk, ADA, LISP, C/C++, VB and others.  My education throughout my coding career has taught me well and that there are several theoretical principles that should be followed when coding.  One of those being what is referred to having "Separation of Concerns" (SOC) or better known as N-Tier design.  The idea being that you separate your application into multiple layers typically 3 tiers (presentation, business, and data) so that if at anytime one of those things needs to change for any reason you can switch out the tier that needs changing. The most obvious example being if you want to switch from Oracle to a much friendlier ;) SQL Server platform you can do so quickly by just changing the code in your data tier and not change any of the other tiers in the application.  So clearly, when I see MVC I think, ok think this could be great as it will provide me a framework to get to a similar level of design.  Frankly, out of the box ASP.Net does not provide a good template for n-tier design and many of the demos when one learns ASP.Net do not show how n-tier is accomplished either. This can be accomplished pretty easily with several projects and/or classes in your solution.  I will not get into the specifics of this should be well documented elsewhere (if not, let me know and I will create a post).  In fact, MVC is really the only project template I have seen from Microsoft which provides an obvious n-tier structure for which to build your application.  Which brings me to the topic of this article which should I use, which is better, and why should I use one over the other?

What you get with Both?
Before I get into the differences between the two models I wanted to quickly point out that they are indeed both ASP.Net.  What this means is you get the ASP.Net framework and the related services for free.  Things like caching, personalization, profiling, security, session state and any other ASP.Net Server services.

Web Forms
As noted in several articles I read while looking into this topic ASP.Net Web Forms were designed to help the Windows application developer make the switch to the web quickly and easily by providing him/her with a model that is similar to that of a Windows desktop application.  ASP.Net provided a model to help deal with the stateless nature of the web and many of the other differences between Web development and Windows Desktop development.  With that in mind Web Forms are then:

  • Control Rich - Many controls from Microsoft and 3rd Party Vendors are available for use in your application to help complete basic repeatable tasks
  • Allows for Rapid Application Development - given the controls and the web form model in conjunction with other features such as master pages once can quickly design and build a web site.
  • Provides Data binding to the controls - again another feature to help with rapid application development and with the advent of the ObjectDataSource one can quickly bind a business and/or data object to a control which further allows for the SOC mentioned earlier.
  • ViewState/Postback capability to help with keeping state

MVC
MVC has a history which goes back most notable to Ruby on Rails.  MVC (per its name, Model View Controller)  does indeed provide a very good model for the structure of any application be it web or otherwise.  After all the there are plenty of development shops around the globe which use MVC successfully,  namely any iPhone developer out there today.  As you can see MVC not only has roots in ASP.Net but in many other languages.  ASP.Net MVC however ties together all the great services of ASP.Net with MVC model which gives those familiar with MVC a platform for which they can now take advantage.  Some of the features of MVC are:

  • Obviously clean separation of concerns (SoC)
  • Enable full control over the rendered HTML.
  • Easy Test Driven Development (TDD) (built with TDD in mind).
  • SEO and REST friendly URL.
  • Easy integration with JavaScript frameworks.
  • Support third-party view engines such as NVelocity, Brail, NHaml.
  • No ViewState and PostBack events.
  • Follows the stateless nature of web.
  • Extensible and Pluggable framework. 

Conclusion
There is a brewing debate within the Microsoft camp now about which is better Web Forms or MVC?  Which is the future direction? Will one replace the other?  It is hard pressed for me to find a good reason for which to use MVC other than my ability to completely replace the view engine with another and the complete control I get from MVC.  However ASP.Net was built around the idea that it is a strong platform that will allow developers to easily, quickly and efficiently build web applications.  In practice MVC has a VERY long way to go in that what I loose by getting full control over the views/rendering is the whole premise behind ASP.Net quick easy, and efficient development of my application.  As pointed out I do still get the many ASP.net services but I am now back to writing FOR loops and HTML that in 90% of the cases I have seem in my career I no longer need to do because I have data-binding and controls I can use out of the box or purchase.  The time I would spend writing and hence debugging my views has significantly increased because with MVC I now have to write this code myself.  So it would be easier to do TDD but now I am writing a ton more TDD code because I have to test all the View code that I would not have to test is say I were using OOB controls.   ASP.Net provides plenty of attributes as well to help be control the use of postbacks and view state and even though postback code might add 20K in JavaScript to my page it saves me a gang of time in not having to worry about state and writing a bunch of code to wire up controls. Not to mention with the standardization of "broadband", 20K is really insignificant, i know ever bite counts but so does my time.   Also as for the "full" control feature of MVC I would get most of what I need by using a little known ASP.Net feature called a CSSAdapter.  Needless to say if you are a developer that like me does not like to spend time coding things you can simply re-use and configure then MVC is not for you.  However if you have to have total control over the view engine and the way your views then MVC might be the way for you.  ASP.Net MVC provides a platform for MVC developers to take advantages of a great platform and still create applications using great tools (Visual Studio).  However,  ASP.Net Web Forms will be a much quicker and simpler route.  One thing to note is MVC or Web Forms will not prevent bad design/architecture of an application YOU as a developer must adhere to best practices and always be diligent in making sure your code is as efficient and structurally sound as possible.  For this developer Web Forms will be the way to go as to be MVC is taking a 8-10 year step back into the past.

Lastly, a good colleague of mine made a very good point as to one great application of MVC in the .Net world.  Many of the .Net Content Management Systems (CMS) today leverage some form of MVC including SharePoint (MOSS).   In MOSS your content type is your Model, your View is the ASPX page you Create, and your controller is the code behind for that web page.  To my colleague’s point one should consider using ASP.Net MVC WITH Web Forms so that you may leverage the best of both worlds!