Saturday, January 23, 2010

ASP.NET: MVC Framework vs Web Forms Framework

It looks like "MVC vs Web Forms" debate heats up.

Here's my take on it:
1) Last time I checked, MVC required 50+% overhead (relative to Web Forms) in terms of the amount of code required to implement the same functionality.
Was that fundamental problem fixed yet?

Let's consider an example of business requirements:
Create a page that allows user enter text, click "Save" button, and then call existing business layer method BusinessLayer.Save().
With Web Forms approach it would take:
- 1 line of ASPX code for textbox
- 1 line of ASPX code for Save button (including Save_Click() call).
- 3 lines of code-behind:
void Save_Click() {
Total: 5 lines of code.

How many lines of code that task would get in MVC?

2) Now about why it's important to minimize number of lines of code:
Lines of code is the best available metric of project complexity.
Yes I know that different lines have different complexity attached to it, but by default I assume that complexity of Web Forms’ line is about the same as complexity of MVC's line of code.

3) Many developers say that it's good to know MVC even if you don't use it (just to be familiar with available options to be ready to jump on it).
I agree. Sort of.
The trick is to know what to learn. There are so many technologies out there so it's not possible to learn them all.
How do I know that MVC would have good return on my time invested?
I don't know it yet. Many developers are passionate about MVC, but relying just on buzz around new technology methodology is too risky: for example, RUP (Rational Unified Process) is nowhere now after all that buzz 10 years ago. Learning it (aside of the most basic concepts) would be mostly waste of time.
And lines of code overhead really kills my enthusiasm about MVC.

I'm open to change my mind though. Especially considering that MVC is getting more mature every year.

See also: Scott Guthrie chimed in with great "About Technical Debate" post and even better follow up comments about advantages of Web Forms vs Advantages of MVC Framework



Mattias Jakobsson said...

If you say that mvc requires more lines of code then webforms you have simply not tried mvc or you have done it very wrong. mvc have always (from the first release) and will always require less code then webforms. The reason for this is that you don't have to deal with form posts and mapping to your domain model. So if loc is the best metric of project complexity then use mvc (or just about any other framework available) instead of webforms.

Dennis Gorelik said...


How many lines of code would developer have to write (typing or using designer) in my example above in MVC?
Web Forms -- 5 lines.

Mattias Jakobsson said...

That example is way to simple. You have to write the exact same number of lines. How many more lines would you have to write with webforms if you add 3 more fields? The answer in mvc is 3. If you want to map it to some class the answer is still 3 (excluding the class itself that will look the same in webforms).

Dennis Gorelik said...


What would these 5 MVC lines be?
Jeremy D. Miller gave this answer:
- 1 line of ASPX code for textbox
- 1 line of ASPX code for Save button / submit button
- 3 lines of controller code:
void Save(InputModel model) {
But he didn't address where InputModel declaration come from.

It looks like that declaring InputModel would take additional lines of code (which bring the total above number of lines needed in Web Forms).

Am I missing something here?

Mattias Jakobsson said...

Yes, you are missing something. The lines would be divided equally and would pretty much be the same as in your example (hence a way to simple example to show anything). Jeremy added that model because it is good practice to call a method with one object instead of a number of parameters. But as you didn't bind it to a model in your example (you should) I didn't do that in my example. So instead of taking that model as a parameter I would just take a string.

But lets say we need a few more parameters and we need to bind that to some kind of model. In mvc you create that model, create input fields for each of the properties and take that as a parameter to the action. While in webforms you have to create the model (as in mvc), create inputs for each field and then manually bind it in your code behind.

Webforms will always require you yo write more lines of code.

Bartek said...

In my opinion, the question is becoming blurred with a lot of the cool features of MVC making their way into Webforms. ASP.NET 4.0 added URL Routing, reduced ViewState, and greater control of the HTML mark-up produced by many ASP.NET controls, now the next version of Webforms will incorporate many more MVC features into Webforms. What’s coming in the next version of ASP.NET Webforms?


About Me

My photo
Email me: