Does anyone beside me just NOT get ASP.NET MVC? [closed]

Compared to Web Forms, MVC is simultaneously a lower-level approach to HTML generation with greater control over the page output and a higher-level, more architecturally-driven approach. Let me capture Web Forms and MVC and show why I think that the comparison favors Web Forms in many situations – as long as you don’t fall into some classic Web Forms traps.

Web Forms

In the Web Forms model, your pages correspond directly to the page request from the browser. Thus, if you are directing a user to a list of Books, you’ll likely have a page somewhere called “Booklist.aspx” to which you’ll direct him. In that page, you’ll have to provide everything needed to show that list. This includes code for pulling data, applying any business logic, and displaying the results. If there is any architectural or routing logic affecting the page, you’ll have to code the architectural logic on the page as well. Good Web Forms development usually involves the development of a set of supporting classes in a separate (unit-testable) DLL. These class(es) will handle business logic, data access and architectural/routing decisions.

MVC

MVC takes a more “architectural” view of web application development: offering a standardized scaffold upon which to build. It also provides tools for automatically generating model, view and controller classes within the established architecture. For example, in both Ruby on Rails (just “Rails” from here on out) and ASP.NET MVC you’ll always start out with a directory structure that reflects their overall model of web application architecture. To add a view, model and controller, you’ll use a command like Rails’s “Rails script/generate scaffold {modelname}” (ASP.NET MVC offers similar commands in the IDE). In the resulting controller class, there will be methods (“Actions”) for Index (show list), Show, New and Edit and Destroy (at least in Rails, MVC is similar). By default, these “Get” Actions just bundle up the Model and route to a corresponding view/html file in the “View/{modelname}” directory (note that there are also Create, Update and Destroy actions that handle a “Post” and route back to Index or Show).

The layout of directories and files is significant in MVC. For example, in ASP.NET MVC, the Index method for a “Book” object will likely just have one line: “Return View();” Through the magic of MVC, this will send the Book model to the “/View/Books/Index.aspx” page where you’ll find code to display Books. Rails’s approach is similar although the logic is a bit more explicit and less “magic.” A View page in an MVC app is usually simpler than a Web Forms page because they don’t have to worry as much about routing, business logic or data handling.

Comparison

The advantages of MVC revolve around a clean separation of concerns and a cleaner, more HTML/CSS/AJAX/Javascript-centric model for producing your output. This enhances testability, provides a more standardized design and opens the door to a more “Web 2.0” type of web site.

However, there are some significant drawbacks as well.

First, while it is easy to get a demo site going, the overall architectural model has a significant learning curve. When they say “Convention Over Configuration” it sounds good – until you realize that you have a book’s-worth of convention to learn. Furthermore, it is often a bit maddening to figure out what is going on because you are relying on magic rather than explicit calls. For example, that “Return View();” call above? The exact same call can be found in other Actions but they go to different places. If you understand the MVC convention then you know why this is done. However, it certainly doesn’t qualify as an example of good naming or easily understandable code and it is much harder for new developers to pick up than Web Forms (this isn’t just opinion: I had a summer intern learn Web Forms last year and MVC this year and the differences in productivity were pronounced – in favor of Web Forms). BTW, Rails is a bit better in this regard although Ruby on Rails features dynamically-named methods that take some serious getting-used-to as well.

Second, MVC implicitly assumes that you are building a classic CRUD-style web site. The architectural decisions and especially the code generators are all built to support this type of web application. If you are building a CRUD application and want to adopt a proven architecture (or simply dislike architecture design), then you should probably consider MVC. However, if you’ll be doing more than CRUD and/or you are reasonably competent with architecture then MVC may feel like a straightjacket until you really master the underlying routing model (which is considerably more complex than simply routing in a WebForms app). Even then, I’ve felt like I was always fighting the model and worried about unexpected outcomes.

Third, if you don’t care for Linq (either because you are afraid that Linq-to-SQL is going to disappear or because you find Linq-to-Entities laughably over-produced and under powered) then you also don’t want to walk this path since ASP.NET MVC scaffolding tools are build around Linq (this was the killer for me). Rails’s data model is also quite clumsy compared to what you can achieve if you are experienced in SQL (and especially if you are well-versed in TSQL and stored procedures!).

Fourth, MVC proponents often point out that MVC views are closer in spirit to the HTML/CSS/AJAX model of the web. For example, “HTML Helpers” – the little code calls in your vew page that swap in content and place it into HTML controls – are much easier to integrate with Javascript than Web Forms controls. However, ASP.NET 4.0 introduces the ability to name your controls and thus largely eliminates this advantage.

Fifth, MVC purists often deride Viewstate. In some cases, they are right to do so. However, Viewstate can also be a great tool and a boon to productivity. By way of comparison, handling Viewstate is much easier than trying to integrate third-party web controls in an MVC app. While control integration may get easier for MVC, all of the current efforts that I’ve seen suffer from the need to build (somewhat grody) code to link these controls back to the view’s Controller class (that is – to work around the MVC model).

Conclusions

I like MVC development in many ways (although I prefer Rails to ASP.NET MVC by a long shot). I also think that it is important that we don’t fall into the trap of thinking that ASP.NET MVC is an “anti-pattern” of ASP.NET Web Forms. They are different but not completely alien and certainly there is room for both.

However, I prefer Web Forms development because, for most tasks, it is simply easier to get things done (the exception being generation of a set of CRUD forms). MVC also seems to suffer, to some extent, from an excess of theory. Indeed, look at the many questions asked here on SO by people who know page-oriented ASP.NET but who are trying MVC. Without exception, there is much gnashing of teeth as developers find that they can’t do basic tasks without jumping through hoops or enduring a huge learning curve. This is what makes Web Forms superior to MVC in my book: MVC makes you pay a real world price in order to gain a bit more testability or, worse yet, to simply be seen as cool because you are using the latest technology.

Update: I’ve been criticized heavily in the comments section – some of it quite fair. Thus, I have spent several months learning Rails and ASP.NET MVC just to make sure I wasn’t really missing out on the next big thing! Of course, it also helps ensure that I provide a balanced and appropriate response to the question. You should know that the above response is a major rewrite of my initial answer in case the comments seem out of synch.

While I was looking more closely into MVC I thought, for a little while, that I’d end up with a major mea culpa. In the end I concluded that, while I think we need to spend a lot more energy on Web Forms architecture and testability, MVC really doesn’t answer the call for me. So, a hearty “thank you” to the folks that provided intelligent critiques of my initial answer.

As to those who saw this as a religious battle and who relentlessly engineered downvote floods, I don’t understand why you bother (20+ down-votes within seconds of one another on multiple occasions is certainly not normal). If you are reading this answer and wondering if there is something truly “wrong” about my answer given that the score is far lower than some of the other answers, rest assured that it says more about a few people who disagree than the general sense of the community (overall, this one has been upvoted well over 100 times).

The fact is that many developers don’t care for MVC and, indeed, this is not a minority view (even within MS as the blogs seem to indicate).

Leave a Comment