Thursday, February 18, 2010

IoC libraries compared

I had just posted this to a question on StackOverflow  about the differences between various IoC libraries.  By the time I was done I realized I had a blog post.

I am prepping a presentation for a usergroup…as such I just when thru a bunch of them. Namely: AutoFac, MEF, Ninject, Spring.Net, StructureMap, Unity, and Windsor.  I was able to get all of these to work but MEF.  But I’m sure it is nothing that Glen Block couldn’t fix.

I wanted to show off the 90% case (constructor injection, which is mainly what people use an IOC for anyway). You can check out the solution here (VS2008).

As such, there are a few key differences: * Initialization * Object retrieval

Each of them have other features as well (some have AOP, and better gizmos, but generally all I want an IOC to do is create and retrieve objects for me)

Note: the differences between the different libraries object retrieval can be negated by using the CommonServiceLocator.

That leaves us with Initialization, which is done in two ways: via code or via xml configuration (app.config/web.config/custom.config). Some support both, some support only one. I should note: some use attributes to help the IoC along.

   About my preference on xml vs code initialization: I started with Spring.Net using xml initialization, later switch to unity with code initialization.  I don’t like xml initialization anymore.  It is too easy to mess up, very error prone, not debugable, and not as testable as I would like.

   On Attributes, which a few support, I’m not a huge fan — but I could be convinced otherwise down the road.  

   Finally on typeof: every time I had to write a typeof I threw up in my mouth a little bit.  I’m sure there are reasons for type of, but since the inception of generics in .net 2.0 I don’t see the need.  I’m looking at you Windsor and Spring.Net.

So here is my assessment of the differences:

Read more: <ELEGANTC*DE>

Posted via email from jasper22's posterous