While my home development computer is fried I’m going to hack away at my backlog of non-code related posts. Today I’d like to discuss some of the advantages and disadvantages of using Microsoft MVC over WebForms.
First of all, MVC is not the end all, magical platform that many people would like you to believe. In fact I wouldn’t even recommend it to beginner developers. However, if you have experience with .NET and you’re willing to put in the time to learn how MVC works, you will find that it offers much more control than WebForms.
Stateless Development
The biggest difference between WebForms and MVC (and one that is often overlooked) is that MVC is a stateless framework. WebForms (and the associated page lifecycle and viewstate, etc) was designed to bring the familiarity of stateful development found in WinForms to the web. Unfortunately for Microsoft, the web is inherently stateless (each HTTP request is independent from any prior request) so a layer of abstraction was introduced to fudge a connection.
Because MVC does not have any abstraction over a raw HTTP request, there are no Page_Load methods or event handlers. Instead, every request is routed to a method called an action. An action may return a static page, a page with data, raw data (xml, json, csv, etc), modify your model, or pretty much anything else you can think up.
Action Filters
Action filters are truly the most powerful part of the MVC framework. WebForms has nothing similar. Action filters allow you to intercept requests before and after the controller action executes. Filters make up probably 80% of the functionality in my applications. Some things I use them for are:
· Authorization/security checking
· Sorting and paging data sets
· Injecting static data (so I don’t have to manually populate my drop downs)
· Injecting menus into every page
· Input validation
· Workflow control (after saving on an edit screen I auto redirect back to the list page)
· Exception handling
· Dynamically associating an action with a view
· Data transformation (raw data collections to json, csv, xml, pdf, etc)
Action filters are basically unlimited in what you can do with them. I’m sure developers will be finding new uses for them all the time. Check out Wes McClure’s blog for some interesting ideas on automating the use of filters.
Model Agnostic
MVC doesn’t care how you create and persist your model. You can use LinqToSql, Entity Framework, NHibernate (my favorite ORM), raw SQL (please don’t!), web services, or anything really. There are no controls tying you to a specific Microsoft data technology. I’ve spent too many days hacking apart ObjectDataSources to work with anything besides a dbml.
Automated input mapping
MVC provides model binders which convert incoming request data (query string or form posts) into whatever type of object you specify. Many examples online demonstrate binding directly to your data model however you should be aware of the associated risks. MVC will not stop you from exposing dangerous security holes. This is something many WebForms developers are not used to thinking about. Don’t be scared by this though, there are several best practices available to eliminate these risks.
Full control over rendered output
When you create a page (called a view) in MVC, you are dictating the raw HTML output. The default syntax is very ASP-like, scattered with many beestings. (<% … %>) Because MVC was designed MVC to be highly modular you can swap out the default ASP.NET view engine with one of the many other available engines. I prefer the Spark View Engine because it provides a very concise HTML-like syntax. It also allows you to write macros which are HTML based scripts that reduce the amount of markup you need to write.
MVC also provides Html Helper methods, which reduce the need to write common HTML tags. Some examples are Html.Button(“Save”) and Html.TextBox(“name”, Model.Name). Html helpers are simply extension methods so it is very easy to write your own.
No Surprises
The built-in controls in WebForms are guilty of surprising behavior. My favorite example is the asp:Panel control which renders as a div until you set the GroupingText property. Then it becomes a fieldset. Surprise! Hope your css didn’t break.
Convention over Configuration
This is my favorite part of MVC. Although it isn’t available “out of the box”, because MVC is so flexible it is easy to add your own features. I have adapted FubuMVC’s Html convention feature to work with Microsoft MVC. This allows me to have dynamic inputs. Instead of writing Html.TextBox(“name”, Model.Name), I can instead do Html.Input(m => m.Name) and allow the framework to use my conventions to determine that Name should rendered as a text box. MVC 2 will provide similar functionality (with Dynamic Data and templating), but I prefer the Fubu approach because it gives you much more control than even a template would. Expect a post with details about this soon.
True AJAX support
Since actions are not required to return HTML, you can use them to return raw data as JSON for AJAX. MVC even comes with JQuery right in the default project. There is no fake postback, like in WebForms, to render partial views. Since MVC is stateless I use AJAX forms to provide the illusion of state to the user.
Testability
Many people promoting MVC are also promoting TDD and testability. This is because MVC almost encourages unit testing. Unlike many people, I’m not going to encourage testing your controller actions, but the ability to is there. Microsoft listened to the community and provided interfaces for nearly everything in MVC so you can stub away if you please.
Because MVC is not only a framework but also a pattern, it is my hope that developers who are not familiar with patterns will learn more about them while learning MVC. My goal for this blog is to help lower the barrier or entry to MVC with real work examples and to promote pattern oriented development.
This all sounds great, but what are the disadvantages? Businesses that have investments in WebForms may not want to switch any time soon for many reasons.
Small, vocal user base
Although it may sound like WebForms development is over, we MVC developers are still a very small, albeit vocal, minority. Despite being small, it is very easy to find people in the MVC community willing to help you learn. Many members of the MVC team at Microsoft are very active in the community and there is no shortage of bloggers willing to answer questions.
No designer support
Developers who are used to drag and drop probably won’t welcome switching to a framework with no concept of a designer. Everything in MVC is hand written, at least in my experience.
Lack of third party tools
There are thousands of third party tools available for WebForms and many companies have invested in them. The MVC community does not have nearly as many tools available. However there are a lot of free JQuery plugins available for grids, select boxes, calendars, etc. You will find yourself writing a lot of javascript with MVC if you want AJAX. JQuery is one more library that developers will need to become familiar with when switching from WebForms.
I’m sure that there are many advantages and disadvantages that I have overlooked, but I hope this gives you an idea of how MVC compares to WebForms and why you might want to check it out.