I’ve noticed a lot of talk about asp.net MVC lately, but I haven’t come across a clear or compelling description of when, where or why I would want to use it over WebForms.
Let’s say I wanted to build a small web application that allows a person to advertise some items online. The website will have 4 use cases:
- Search adverts
- View listings
- View item
- Place an advert
Let’s assume:
- I’m not particularly interested in unit testing my controller. The page will either render the list of items correctly, or it won’t.
- I am interested in more control over the HTML markup.
- I’m not interested in using the latest buzz technology just for the sake of it.
- I am interested in using the tool that is best suited to the job in terms of productivity, performance, maintainability & simplicity of the end solution.
- I don’t want to have to work around a bunch of nuances to get something simple to work.
So, my questions are thus:
- What are the fundamental differences between the two models?
- In which scenario is one better than the other?
- What are the gotchas with asp.net MVC (I’m aware of the gotchas with WebForms)
- For our sample app, what would I gain by using asp.net MVC instead of WebForms?
- For our sample app, what would I lose by using asp.net MVC instead of WebForms?
- Is it feasible to mix and match models within the same small application?
Thanks to anyone who spends the time to contribute an answer.
WebForms try to mimic WinForms development by allowing you to reuse lots of pre-made controls, and by faking web application state via the hidden _VIEWSTATE mechanism.
MVC is a pattern designed to help you separate your data (Model), business logic (Controller) and presentation (View). It adheres more to the true nature of the web : RESTful URLs, stateless.
In my opinion, for an intranet application making heavy usage of controls, WebForms can be useful at reducing development time, because thanks to the designer you can create your UI very quickly and let the framework manage the app’s state automatically.
For any other project, especially a public website, even a small one, I think MVC is the way to go.
I’d say there is some learning curve to fully understand the MVC pattern and its power. Also, since the framework is still in BETA you can expect the API to experience some minor changes before release.
Since JavaScript is not hidden from you in MVC, it would also require some time to learn if you’re not familiar with it. jQuery greatly simplifies this though.
You’d gain better control over HTML markup and Javascript behavior, a cleaner separation of concerns and some easily testable codebase (even if you don’t seem interested in unit testing it).
You’d lose the ‘drag and drop’ quick way of building your pages and the application state management.
In some ways, yes it seems.
I’d recommend watching this talk by Phil Haack, who gives a good overview of the framework and invites Jeff Atwood to talk about how he built StackOverflow with it.
He explains how SO is using some WebForms controls for CAPTCHAs which render themselves into the view.