Wednesday 14 October 2009

Models, Views and Controllers

Pork Scratchings
Another nerdy post, I'm sorry. To make up for that, and because I was glued to every one of Jamie Oliver's adventures in the US, I have a gluttonous photo for you. I bought a bag of pigskin from the Farmers' Market on Saturday and made my very own Porky Scratchings from it. I used the recipe from Jamie's America, which involves stirring cayenne, cinnamon, seasalt, honey and garlic into the cooked scratchings and putting them back in the oven for 8 minutes. This was gorgeous but a little too much like Nigella Lawson's Union Square Café bar nuts from Nigella Bites (same topping but with brown sugar and rosemary). Next time I'll try just fennel, garlic and seasalt.

Well, for some time now, I've had the first chapter of a book on ASP.NET MVC on my desktop. It's about a site called NerdDinner. For some reason, I decided to actually take a look at it and started to work through the example. And I instantly fell in love! I've known I want to program in C# for several years now, and that I feel comfortable in Visual Studio. I am also aware that there are things called "Content Management Systems", lots of them, and only one of those written in .Net. What I wanted from a CMS would be a way of managing content (of course), so the customer can change what is on the page, keeping it up to date. I would therefore need a membership system. I am aware that it is important to keep data storage well away from the page itself, so that database tables and the classes that represent them can be looked after. The problem is that what we can now call the "traditional" ASP.NET structure, with code behind pages and databound gridviews is that it is easy to get into an almighty mess, which at some point needs to be sorted out. Then it looks like way too big a task.

I looked at various "Content Management Systems". Why are they called that anyway? Why not "Website Management Systems" or something else. Wordpress is supposed to be easy but I didn't get on with it, as it is pretty rigid. Fair enough, it was designed for blogging anyway. I investigated Drupal. Huge learning curve and writtten (like many CMSs) in PHP. No thanks. I looked at DotNetNuke. Two problems: it dumps a big load of mysterious files and language in your website, and seems to require a whole database all to itself. Third problem, I could in no way get this to work on my Vista PC with SQL Server, with or without IIS. Forget it. This was disappointing, as I really would prefer to stick with Visual Studio.

I tried Django late last spring, using various books and the help on the Django site. Python is a language that certainly appeals, it is clean, nerdy in a good way and does not carry all the bloat (written in various styles) that PHP does. To my frustration, I could not get beyond the most trivial examples. Neither could I work out where to keep my CSS, images and Javascript files. The writers of all these books seemed to regard the matter as unimportant. Examples were written with inline CSS (yuk!).

So, ASP.NET MVC. It doesn't call itself a "Content Management System", any more than .Net itself does. It's a Framework. That sounds much more open. As long as you have C# 3.0 (check) and some kind of MS SQL database (apparently Web Developer Express 2008 will do) you're in.

What appeals to me is that so many other modern buzzwords and ideas seem to be a part of this. They have copied ideas from Ruby on Rails (I had tried that as well and found it difficult, now I would be back in my comfort zone). It was based on the old-fashioned but newly discovered pattern "Model, View, Controller". This separates the pretty web pages from the content in the database and is all run with a set of "controllers". There is a huge emphasis on testability, even designing with behaviour and testability in  mind from the start. You are encouraged to import and use open-source test tools such as NUnit and Moq (I'm learning). The great JQuery library is part of the framework.

As for the parts I already know well, CSS can go anywhere the views can find it (typically in a "content" folder. Web pages are built up using a master page and come out with nice clean html. Any javascript or id attributes are what I put there myself and not generated out of monolithic .Net widgets.

Unless I want them of course. MVC is not the dark side of the .Net. You can (fairly) easily add in bits of web form-type ASP.Net when you want to.

I'm so happy, I'm getting  incoherent. Once I've worked through the superb Steven Sanderson book Pro ASP.NET Framework, I'm going right back to my portfolio and rewriting everything in MVC. It's light, it's programmer-like and I love it.