Professional ASP.NET MVC 1.0

Book review • ASP.NET Web Forms is dead, only it isn’t really, because ASP.NET MVC is just different, and not a replacement, but it’s better, except for when it isn’t. So what do we do with it? Are ASP.NET Web Forms developers losing their job security if they don’t retrain? The answer is no, but it’s easy for a skilled developer to pick up ASP.NET MVC, especially if they have worked with web development pre-Web Forms. This book explains what ASP.NET MVC is all about.

Professional ASP.NET MVC 1.0
Rob Conery, Scott Hanselman, Phil Haack & Scott Guthrie
Wrox • ISBN 978-0-470-38461-9

I’ve been developing in ASP.NET for around 3 years now, and initially it was a strange experience. Before that, I was a developer of C# desktop applications, so knew the .NET Framework well. My web development experience, however, was a mix of technologies that I had played with on my own time. From static HTML pages, then on to CGI scripts, PHP, mod_perl, Java Server Pages, and finally Ruby on Rails. All of these had something in common with each other – something that ASP.NET lacks.

Because, when making ASP.NET, Microsoft wanted to emulate the event driven model familiar to VB6 programmers. But that’s not how web servers work, so ASP.NET has to jump through lots of hoops to come up with a Windows Forms-like model. So-called Web Forms work well most of the time, but it is a very leaky abstraction. With any moderately advanced use, you need to be aware of what is going on behind the scenes, otherwise you’ll end up wondering why a line of code doesn’t work if its in Page_Load but does in Page_PreRender, or vice versa, or wondering how to fix a ViewState exception. ASP.NET Web Forms does some very clever things to make the event-driven model work, which makes me think that Microsoft has produced something which is either a work of genius or is insane – and I more frequently think the latter.

So ASP.NET MVC is something of a “back to basics” for ASP.NET developers. Web Forms are gone, and instead is replaced by a Model-View-Controller (MVC) framework a la Ruby on Rails. Instead of viewstates, postbacks and events, you have separation of concerns, URL routing and testability. It’s a much simpler, lighter-weight system, but should be able to match or beat Web Forms for results.

To teach us how to switch to MVC is this book. It’s written by four Microsofties who were involved to varying degrees in the development of ASP.NET MVC – Rob Conery, Scott Hanselman, Phil Haack & Scott Guthrie – so you can be sure that the authors know what they are talking about.

The first chapter – which makes up a significant 160-page chunk of the 400-page book – is a complete walkthrough of making an ASP.NET MVC application. The application is NerdDinner – which you can see online at www.nerddinner.com – a tool to get groups of geeks together for pizza and beer (or regional equivalent.) In addition to all the standard CRUD stuff, it features security authentication, AJAX and online embedded maps. What more could a hungry nerd demand from a website? This chapter is rich in screenshots, almost literally holding your hand as you go creating the NerdDinner website for yourself. It covers nearly all ASP.NET MVC major functions in the process. Reading this chapter might be all you need to know about ASP.NET MVC. In fact, this chapter is available free online, and so is the source code, so go and have a look at it yourself.

The rest of the book moves back into the kind of style you would expect. The major functions of ASP.NET MVC have a chapter each. In each chapter the basics are described, then quickly you move into more advanced functions that NerdDinner didn’t exercise. ASP.NET MVC functions for routes, controllers, views, and filters each get their own chapter.

A further chapter rushes through AJAX. It assumes you already know about AJAX, and instead concentrates on how ASP.NET MVC supports AJAX, with a bit on the jQuery library that is now bundled with Visual Studio 2008 SP1.

The next chapter talks about security. Similar to the AJAX chapter, this is about how ASP.NET MVC helps you with security. Unlike the AJAX chapter, it talks more about the basics, instead of assuming you know it already. Cross-site scripting, cross-site request forgery and cookie handling get a write up. Following this the ASP.NET MVC-specific issues get discussed, such as how to ensure your controllers are secure.

Following are a couple of chapters on test-driven development. One of ASP.NET MVC’s advantage over web forms is that it is easier to unit test – there are better uses of interfaces to allowing the use of mock objects, and key classes in your project can stand alone and be tested in isolation. This chapter gives a whirlwind introduction to test-driven development. A subsequent chapter also covers testing, describing patterns and techniques to make the code you write testable too.

A final chapter covers how to bring ASP.NET MVC into existing Web Forms projects – things you need to be aware of, as well as the nuts and bolts of which assemblies you have to reference and what you need to add to Web.config.

It’s slightly incredible that this book made it to press so quickly after ASP.NET MVC was finalised. Unfortunately, it seems that in some parts the book were rushed. Some concepts appear to be introduced slightly out of order – for example, a short section of named routes appears in the routing chapter, and it’s not at all clear why you would want to name routes. It only when you are reading about views later does it become clear that named routes can be useful when creating hyperlinks. I spotted a couple of typos as well, but not very many. I get the feeling that although most of the book may have been edited in good time, parts of it were changing late on and were not as comprehensively reviewed. Some of the functions are explained very rapidly – a concept is introduced, explained, demonstrated, and concluded all within a page or two. String a couple of these together and I sometimes felt out of breath after reading them all.

The book frequently contains product team asides, call-out boxes that explains what the ASP.NET MVC development team were thinking when they made a particular thing work like it does. These insights are often useful and interesting. Despite having four authors, the feel of the book hangs together generally fairly well. Occasionally it becomes slightly too informal with the authors name-checking each other – not necessarily a bad thing, but a bit of a distraction.

In the end, there is no doubt that this book is packed full of very useful information. NerdDinner acts as a comprehensive introduction to ASP.NET MVC, and then just about any question you have is answered in the following chapters. The later chapters are weaker, especially the chapter on testable design patterns – it doesn’t cover the subject in enough depth to be able to practice the concepts properly – but this isn’t a book about testing, and the interested reader should definitely consult other in-depth resources for this subject.

The NerdDinner chapter is a significant and valuable part of the book, and having the NerdDinner chapter freely downloadable on the web does diminish the value of the book somewhat. Given this, the book’s RRP of £33.99 is quite steep. However Amazon UK are selling it for £22.09 (at the time of writing) which is much better value. If you are serious about wanting to learn and use ASP.NET MVC – and I think most Web Forms developers should seriously consider this – then this is an excellent book for you.