Unity 2.0 – Just in the nick of time

We’re just re-architecting our site currently and a big part of that is test first (I daren’t call it test driven!) development. It’s an MVC2 project, with distinct business and data access layers. I’m loving MVC2, model binding, and the power I’m getting from making my domain models richer.

Obviously one of the biggies in unit testing is the use of Inversion of Control via an IoC container (older list) so that you can reliably pass around interfaces rather than implementations, the aim being to improve the testability of it all, and allowing mocking up of dependencies in order to test just certain areas of the functionality.

I’d investigated a few IoC containers:

CastleWindsor – I just personally found this too much like hard work in terms of the thinking side of things – I didn’t like the syntax, which must be an utterly personal thing as there’s not a big difference between that an Unity.

StructureMap – this one felt more like voodoo, though worked well – was definitely a good second choice.

Unity (v1.2) – I liked the config on this one, and I liked the way of specifying which constructor to use in a multi-constructor dependency. What I didn’t realise was that it didn’t out of the box seem to support the ‘PerWebRequest’ type lifestyle on dependencies (though I managed to roll my own without too much difficulty).

I’d hasten to add the above are my findings, and may not be fully representative of someone with a clue playing with this! I may easily have missed elements in the initial plays.

Unity 2.0 (which is available as part of Enterprise Library 5) comes with a ‘PerResolveLifetimeManager’ out of the box now, which does exactly what it says on the tin – during a running instance (in this case, a web request) the first time it resolves it’ll new up an instance for the app, and during that instance it’ll use that one, until the next instance (page load).

Hi Terry
Somebody asked me about polymorphism the other day.
Its a word I first heard aout 15 years ago along with Inheritence and Encapsulation, probably while reading a book on Smalltalk, much good that did me. Inheritance and Encapsulation were fairly digestible, but Polymorphism just seemed rather abstract at the time (funnily enough, it turns out to be closely related to ‘abstract’ after all) So it became a word I relegated to the dustbin.

Fastforward 10 years and I’m now reading books on C# and MSDN documentation. Of the language used to describe this language, I’ve found the word Polymorphism is little used, thankfully, but the word Interface has now entered my world.

Whilst not as obscure as the ‘P’ word, the ‘I’ word took some getting used to. Finally I’m getting to the point of this post, one of the best demonstrations of Interfaces (and IoC) I’ve seen is from Mike Taulty. There’s 8 videos, but its the first two that take you on a magical journey from a simple tightly coupled program to an interface based loosely coupled program held together by Unity.

If you have to explain the idea/benefits of IoC to a newbie I recommend these two videos: