Today, InfoQ publishes and excerpt (PDF) from Test-Drive ASP.NET MVC written by Jonathan McCracken. We also used the opportunity to interview the author.
Test-Drive ASP.NET MVC gives a thorough introduction to ASP.NET MVC using Test Driven Development (TDD). In addition to cover the expected areas within ASP.NET MVC the author also uses NHibernate as backend store, REST for communication and show you how to effectively deploy the final solution in a repeatable manner. The book is targeted at both existing ASP.NET developers as well as non Microsoft developers.
As part of this interview, InfoQ also got a coupon code that will give our readers 25% discount on the book. Use the code InfoQTestDrive at http://www.pragprog.com/titles/jmasp/test-drive-asp-net-mvc to take advantage of this offer.
InfoQ: What motivated you to write Test-Drive ASP.NET MVC?
Jonathan: I've been developing in .NET for the past 9 years and came across ASP.NET MVC about two years ago while teaching an C# bootcamp which used it. Having developed in Ruby on Rails on other projects I was excited to see Microsoft come out with something similar. It was the first time in many years that I was genuinely excited by a Microsoft product and wanted to tell the world about it. I figured a book might help with that.
The other big motivator was test driven development. One of my favorite technical books is Kent Beck's Test Driven Development By Example and am passonate about the subject. I also was always frustrated with most technical books approach to covering testing near the end of the book (say chapter 9 or 10). I thought the approach to going Test-First right from the start all the way through to the end. That way you not only learn the MVC framework, but how to test it.
InfoQ: In your book you mention that experienced Microsoft developers might be unfamiliar to the emphasis on TDD. Why do you think that is?
Jonathan: I blame Visual Basic. Microsoft has done a great job in marketing its tools and products to a demographic of developers whom are not familiar with
techniques like TDD. There are thousands of Microsoft developers out there whom use the tools that Microsoft (or other vendors) provide and not aware of the
other options that exist. That means up until now that TDD, dependency injection, and open-source tools weren't on the radar.However, times are changing as more and more corporations adopt these techniques and tools because they've proved their effectiveness. One of the audiences of this book was developers whom are unfamiliar with ASP.NET MVC and TDD. This book can teach you both.
Course, there is a group of .NET developers who associate themselves with ALT.NET whom are both experienced Microsoft developers and are not only aware of TDD but practice it daily.
InfoQ: Microsoft was late to the game introducing customers to a MVC framework and had a golden opportunity to pick features from existing MVC frameworks. In your experience, is there anything you’re missing in ASP.NET MVC compared to other frameworks?
Jonathan: First off I think it was a smart move to decouple ASP.NET MVC from any persistence framework. I always disliked how I had to use ActiveRecord with Rails (I know that has changed in Rails 3.0). As a developer I want to choose the persistence mechanism that works best for my project - so if that's MogoDB or Oracle or NHibernate.
As far as what's missing I think the 2.0 version of MVC could use a better validation model. I prefer not to have my models automatically validated when they're passed into a controller action. The ASP.NET MVC team is working on addressing some of this in 3.0, but we'll see what they finally come up with. MVC 2.0 also is missing an updated View Engine which leaves the markup a little more verbose than it needs to be - that's being fixed in 3.0 with the Razor engine which looks a lot more like ERB in Rails.
The other thing that is missing is Dependency Injection. They've done a good job with the Factory pattern, and allowing developers to hook in their
framework of choice but didn't include it out of the box. The thing that worries me slightly is the 3.0 version is planning to include the Common Service Locator project to hook in dependency injection all over the place. While it some ways this is a step forward people often abuse it by including static in-line calls to the service locator, you end up with stuff like:public void SomeMethod()
{
Locator.Get<Service>().DoSomething();
}Ugly stuff from a testing point of view. Hopefully the MVC team in Redmond has heard the cries against this and will implement more Factories. If I had it my way they'd just include Windsor Container as the default Dependency Injection framework...but I don't want to start a religious war ;)
InfoQ: What would you say is the major difference between ASP.NET MVC and other MVC frameworks on other platforms you’re familiar with?
Jonathan: I think ASP.NET MVC is a more pure presentation framework than most. Other web frameworks include everything you need, which has some pros and cons. For example you don't always get to choose what persistence framework you want to use - sometimes that's dictated by corporate policy. This won't be a problem
for ASP.NET MVC developers as it doesn't ship with one.On the con side it can be disorientating for newcomers who have to pick and choose which components to use. That's one thing I put some effort to explaing
in part III of the book. Showing developers not just the tools to integrate with, but an approach for integrating with 3rd party tools.
InfoQ: Do you think developers skilled in other MVC frameworks will feel at home with ASP.NET MVC?
Jonathan: I think so - the complaints I hear mostly from developers from other languages/frameworks is getting used to Visual Studio and C# syntax. Once you get over that hump, you should find the transition pretty smooth. Course, buying a good book to guide you through this doesn't hurt ;)
InfoQ: ASP.NET MVC has been around for a few years now. What’s your impression of the adoption rate in the community?
Jonathan: Well in terms of numbers it's still in its infancy - about a million downloads from last I heard. I think you'll see this steadily increase over the next
few years as it eats up market share from ASP.NET WebForms. Microsoft's official position is that it will support both web frameworks, however I predict
that ASP.NET WebForms will slowly die off.I see ASP.NET MVC also making a small dent into the markets in which the open-source frameworks like Django and Rails play in. The reason for this is two-fold. First and more significantly is the commercial support that comes from a brand like Microsoft which makes it more corporate friendly. While frameworks like Rails are certainly fast and developer friendly there isn't a major public company backing them up. The second reason is because the changes in the framework itself are more evolutionary. Migrating from MVC 1.0 to MVC 2.0 is a walk in the park compared to Rails 1 to Rails 2.
InfoQ: With ASP.NET MVC 3 Preview 1 Microsoft made the new view engine Razor available. What’s your impression of this new view engine?
Jonathan: I like it a lot. We'll see how far they take it, but from my initial reaction is very positive. Razor cuts likes of code like...well a razor. Also,
it leverages all the stuff I know about HTML Helpers so I'm not having to go back to square one.Ideally, I'd like to see them take it a step furthur and stop developers from doing multi-line blocks like if/else statements. In my opinion those things belong in testable HTML Helpers, however I understand why they included them - to satisfy the practice of putting logic in the views. I think views should be dumbber than dorenails so that I can sleep better at night knowing that more of my code is tested.
InfoQ: In your book you also focus on deployment and make use of Migrator.NET (inspired by Ruby on Rail's Migrations) to deploy database schema changes. What would you say is the biggest advantages of using Migrator.NET over more traditional SQL scripts?
Jonathan: Yeah I like the migrations in Rails and personally have always disliked SQL. Why? Because it gets you thinking in rows and columns, not in terms of
objects and interactions. So anytime I can minimze my dealing directly with the database in its raw form I opt for that.With Migrator .NET is you can stay in C#. It also keeps your upgrade and downgrade more conviently packaged together. I'm also a fan of dbDeploy.NET - but given the choice I'd rather stay in C#.
InfoQ: Any last minute recommendations for experienced developers considering or starting to use ASP.NET MVC?
Jonathan: If you're making the switch to ASP.NET MVC take advantage of a more effective mode of development with Test Driven Development (TDD). If you're not using TDD with ASP.NET MVC you're not realizing the full power of this new framework.
I'm like Kent Beck, TDD guru, and consider myself an average developer. Test Driven Development, along with some other Extreme Programming practices, turn me
into an above-average one. The techniques are simple enough to learn, and that's really why I wrote the book - to make it easier for others to use TDD in ASP.NET MVC.
InfoQ: Thanks for taking the time to talk to us.