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:
Also, Ninject has the coolest web site of all the containers.
(Initialization code or Xml or Attributes) Version 2.5 is also very lambda’y. All in all, this is one of my favorites. Some very interesting ideas around how StructureMap uses Attributes.
But, here is my StructureMap initialization code:
Also, you can also go nearly config-less with StructureMap. This is the Attribute driven style. You litter all of your classes with the attributes Pluggable and PluginFamily. Then add a file to your project called StructureMap.config that looks like this:
After that you are good to go. I kind of like this approach…but I feel dirty afterwards.
If you want to initialize via xml, here is what to look forward to:
That was by far the largest amount of xml require to initialize any of the containers. I’m hoping Chris Tavares can fix that down the line.
Actually, my biggest gripe with Unity is the name. For all the other IoC libraries, if I google the name I get their web site as the first link. Not Unity. This make Unity the hardest to find info on.
Anyway, here is a sample of the xml from the app.config.
Also, without the Common Service Locator, Spring.Net is not generic friendly. Another gripe, the download expands to 120 meg (you don’t need all of it). They include every single thing under the sun (.Net 1.1 and 2.0 asseblies, tutorials, extras, addons, docs, kitchen sink), which makes it a bit more daunting than it is.
(xml and code).
This one just rubs me the wrong way. No support for generics, configuration is a pain no matter what (to me). But it also does anything you could want it to do. (Edit: I’ve been getting a lot of heat from the Castle Windsor Right-Wing Conspiracy movement. So I took another look at the api — and realized I was wrong. Sorry, my bad. I’ve updated the code using a better api. Words to remember: Reflector is your friend — not ReSharper’s intellisense).
Here is a sample of in-code initialization:
Or, here is my code from my app.config:
(code and xml). Nice simple IoC library. Seems to do the basics with not much fuss. Here is how you initialize it:
xml configuration looks like this:
Which also change the code initialization to read the xml:
Fun tidbit about this one: the main contributor for this project is a Microsoft employee — and the code is on Google Code. Why not CodePlex? I have no idea.
My final verdict (for just this minute)
If I had to choose today: I would probably go with StructureMap. It has the best support for C# 3.0 language features, and the most flexibility in initialization.
If I was in a non-opensource friendly environment, I would go with Unity (or MEF, but I have to get it to work first).