Q&A

Decoding Microsoft's MVC

Computer Zen's Scott Hanselman enlightens developers.

In July, Scott Hanselman marked his one-year anniversary as a "blue badge," the same month Bill Gates retired from day-to-day operations at Microsoft. After six years as chief software architect at online banking company Corillian -- now part of CheckFree Corp. -- Hanselman joined Microsoft's Developer Division as a community liaison for Scott Guthrie's dev teams: ASP.NET, IIS, Silverlight and Windows Presentation Foundation, among them. His current title is senior program manager but the job of "basically spreading the love" remains, as he hails the group's tools and technologies by way of tutorials, screencasts, podcasts, code samples and, of course, his popular blog, Scott Hanselman's Computer Zen.

Part of that message is the introduction of the new-to Microsoft anyway-Model-View-Controller (MVC) framework for ASP.NET 3.5. The framework enables Web app developers to separate the data and business logic (model) from the UI elements (view) and communications (controller). Proponents say this request/response model fits the stateless Web, unlike the events model (ViewStates and Postbacks) traditionally used in Windows desktop apps and WebForms. ASP.NET MVC Preview 5, made available at the end of August on CodePlex, is the final test version before the beta, expected sometime this year. Senior Editor Kathleen Richards caught up with Hanselman to learn more about life at Microsoft-he works remotely out of his home in Oregon-and the MVC technology.

Scott Hanselman, Senior Program Manager, Microsoft "Some people may feel like they're sitting at a fork in the road and they have to pick one and make the right decision-that is a non-existent crisis."
Scott Hanselman, Senior Program Manager, Microsoft

You've been at Microsoft during an eventful year. Bill Gates retired from his day-to-day role. The company is openly contributing code to open source projects. What's it like working for Microsoft these days?
I think that there's less fear about open source now and the MVC project is a good example of developing out in the open. Phil Haack is the program manager of MVC and he and other guys and gals on the ASP.NET team are kind of trying this as an experiment.



The source for .NET was made available for debugging purposes recently and that was spearheaded by Shawn Burke. And I think by getting that out there, not necessarily as open source, but as 'source opened,' if we're going to eventually make that available for people to look at-why not a project like MVC, where people can watch it being developed. The feedback loop is much, much tighter-you put the source out and someone can say, 'Gosh I don't like what you did here on line 30.' That's a pretty powerful thing. But that doesn't mean that Microsoft is going to start taking patches directly from the community for something like MVC, for intellectual property reasons.

We can still give you the source and you could apply the patch yourself to your own local copy. So not only has the source been made open for MVC, but it has been made what is called 'bin deployable.' Which means, say, you have three different Web sites all on the same machine, you could have this framework and you find a bug. You patch and fix it, so you have a license to build and deploy a private version of the framework. The idea is-here's the fundamental thing that's changing at Microsoft-the public wants certain things and people at Microsoft want to give them that stuff.

The key benefit of MVC frameworks is a separation of concerns, which facilitates unit testing and test-driven development. What are some of the other advantages?
For some people that may not understand what separation of concerns means-the idea is with WebForms, you've got a system of abstraction that is set up to hide certain things from you. Its primary concern is to make deployment very rapid and very effective, leveraging this visual designer. I can drag data grids over. I can drag data source over. I can double-click on a button and suddenly, all of this is hiding HTTP from you, hiding the underlying protocol-it's writing the HTML for you for the most part.

But there's a class of developer who doesn't want that layer of abstraction. They want to see that protocol.

To your point about separation of concerns, in WebForms, it's less clear when you're designing a system, who's responsible for what and there's nothing that separates the concerns-the idea that this is responsible for HTML and that is responsible for my databases. Things can get a little mixed up because there's no formalization of that.

MVC is a formal attempt at separating the concerns. Views present HTML: that's their job, they don't do anything else. A controller moderates this conversation that goes on between the view and the controller and the controller and the model. And that formalization makes a lot of people really happy, because then I can go and add on unit testing or even better, test-driven development where I write my tests first, and then write the functionality that allows the test to pass. That kind of test-driven development is very, very difficult in WebForms. It wasn't even around when WebForms was coming up.

I can actually have hybrid apps. A lot of people don't realize that it's not either/or, I can have one application that is simultaneously WebForms and MVC. What this means is that you could potentially introduce MVC-like patterns to a few pages on your site while maintaining the functionality. So for example, CodePlex, Microsoft's shared-source repository, is a hybrid; it's part WebForms and part MVC.

Are there certain types of apps that MVC is better for?
Really, really complex, order-entry, data-entry applications I could see being a lot easier with WebForms, probably because of the control model. I could purchase a third-party grid control, third-party reports and all those things and plug them in because WebForms is a very formalized component strategy.

On the MVC side, that's a tough one, you can do anything in either technology-it just depends on how you're going to go about doing it.

In MVC, I don't have postbacks and [ViewStates] and events. I've got the raw HTML and the raw HTTP, but in the new world order, I've got user interface controls that are really client-side. So I have a sortable grid, but a lot of the JavaScript would be happening in a JavaScript way-sort of like Gmail. I might use something like JQuery UI, or some of the AJAX control toolkit; these are controls that have a lot more work happening on the client. I'm going to ultimately accomplish the same goal, but I do it in very different ways and the underlying technology would be very different.

How does ASP.NET MVC stack up against Ruby on Rails?
It's not apples to apples. Rails is not just the MVC framework, it's also a data-access model. They allow developers to use the ActiveRecord [Object Relational Mapping] pattern. There are lots of similarities but there are also other MVC frameworks on ASP.NET that have had these similarities as well. Monorail was the first open source ASP.NET MVC framework. They added an implementation of ActiveRecord. But just as you can plug in different components on the Rails side, one programmer might prefer some gems-plug-ins-while another programmer would prefer others.

I see a lot of user discussion about 'pure MVC' and keeping the framework client-agnostic-is that the plan?
The team is totally committed to plugability and choice. So for example, there's nothing in MVC that says you must use ASP.NET AJAX. There's nothing in MVC that says you must use LINQ to Entities. If you want to use whatever combination of whatever you want to use, take as little or as much as you want.

The first community technology preview appeared last December and preview 5 was released at the end of August. What's on the short list before version 1 is ready?
If I understand it correctly, [Phil Haack's team is] working on some AJAX features. They're working on figuring out what the localization pattern is and I think they're working on formalization of the component model. Performance testing and all of the standard quality gates that one has to do when one releases a product like this. They're getting pretty close. Q4 calendar year 2008 is the goal for shipping it.

How would you characterize the learning curve for adopting the technology?
It depends on what capabilities you already have.

One can learn MVC as just a framework within a few days. It's not MVC that's going to slow down your learning-it's the ecosystem of tools and methodologies around MVC that's going to slow you down. So you have to decide, how do you want to take a bite out of this? Do you start with unit testing and work your way up? Do you jump in with both feet? MVC is just one Tetris piece in a larger puzzle of tools like Moq, Rhino Mocks, TypeMock, the mocking framework stuff, NUnit, CUnit, MbUnit and other methodologies, not just test-driven development, but also behavior-driven development. If these are concepts that you're unfamiliar with then you've got to spin up on them as well.

If I've got to build a Web app in the next six months, should I go with WebForms or MVC, or both?
If I were starting a new app tomorrow, the decision for me would be to use MVC completely, recognizing that there will be some places where that will be difficult on certain kinds of interactions. I could see someone making a blogging engine, and having the public-facing part of the blogging engine be MVC, but maybe they want the administration console to be done with ASP.NET and Dynamic Data because the editing and the leading of posts lends it to that interaction model.

The Visual Studio team is working on MVC integration?
I know that they have created a feature template and they have put some hooks in the project template where as an open source author you could insert some custom templates. There may be a new controller directly from Visual Studio. But, you know, Visual Studio includes a lot of the integration that you would need-like being able to run unit tests from the IDE. You can still use the Visual Designer using the WebForms View Engine.

What is most important for an enterprise-level dev team manager to realize about ASP.NET MVC?
This is just another choice and this does not in any way represent lack of commitment on the part of Microsoft to WebForms itself. Some people could say, 'Well, Microsoft must be taking people off of project A and making them work on MVC.' Microsoft doesn't have unlimited resources, but they definitely are into having WebForms and MVC be awesome. So it's new. It sits on top of ASP.NET. It is not a replacement for anything. Some people may feel like they're sitting at a fork in the road and they have to pick one and make the right decision-that is a non-existent crisis.

ASP.NET MVC requires ASP.NET 3.5?
If you go to my blog, I got permission for people who may be stuck and haven't upgraded up and they want to use ASP.NET MVC under .NET 2.0. I put some hacks and may be able to get that to work. It's kind of unsupported-you're on your own. But if you have a project that you want to get started on immediately and you don't want to upgrade for a couple more months, you can at least start your prototype on .NET 2.0 using MVC until you move to the fully supported version running ASP.NET 3.5 running under SP1.

About the Author

Kathleen Richards is the editor of RedDevNews.com and executive editor of Visual Studio Magazine.

comments powered by Disqus

Featured

Subscribe on YouTube