Wednesday, April 08, 2009

MVC.Net, Structure Map, and TDD

So I started to watch some webcasts on   One of the series really interested me because the guy was going to create an entire app from the ground up following TDD, using Linq to SQL, Structure Map for Dependency Injection, and MVC.Net for his UI.

Why did this interest me?   Well I have been using NHibernate and Spring.Net and I was curious what these other technologies could do.  I really was not interested in the TDD part but surprisingly something said in the webcasts, or maybe a comment on the blog really got me thinking about it, and I think I have seen the light.

First TDD
Like I said I never really bought into Test Driven Development.  I always thought as long as you have unit tests it did not matter when you wrote just have them.  However I noticed that as he wrote code to make tests past, that is all he did.  I started thinking about all the time I wrote code based on a gut feel for what I would need, only to have wasted code.  This method kinda stops that from happening cause if it wont help the test pass you don't need it.

Another interesting point discusses was that TDD will not guarantee code correctness.  All it does for each test given some input an expected result occurs.  Yet you can still rely on those tests to tell you if things go bad.

That brings me to my next topic.... Dependency Injection with StructureMap

To do really good unit testing it helps to separate your concerns.  Using a Injection tool really makes this a simple task to perform.  I have been using Spring.Net.  I really wanted to look at Unity 1.1, but after reading some blog post, I was turned on to StructureMap.  Let me say after working with it for two days, and reading about it for a few more, I really love working with it. 

I like the fact StructureMap has a fluent interface, and that all the mappings can be tested.  StructureMap provides some testing interfaces.  For example you can call a method on the container and it will validate you mapping setup and if it fails it will output why in readable human text what object it has and what it was missing from the container.  StructureMap also provides a method on the ObjectFactory called WhatDoIHave.  This method will display in readable language everything in the Factory.

Another feature I like is the fact you can pull objects from the factory even if you haven't mapped them.   The factory will still return the object to you injected with anything it can from the mappings.

I have to say I am really digging MVC.Net.  I have worked with ASP.NET for a long time.  I have learned the page stages, along with enjoying the dragging and dropping of controls.  Even though there are no controls I love how mvc really separates things out.  I like how easy it is to test ui controller logic.Even though I haven't tried this yet, I like that I can change the T4 Templates or even add my own.  MVC.Net is very robust in the features.  The one thing I do have trouble with is routing.  I seem to have a million routes in my application.  Part of it is the fact that I use complex keys for things and am passing multiple values around.  However, I still find it hard to write nice generic routes.  I think a cool tool would be one that would parse a site and generate some routes for you once it was done.

Post a Comment