I'm trying to expand my mind around dependency injection in .NET (beyond the two frameworks I've personally used) and an starting to put together a list of .NET Dependency Injection Containers and IOC resources.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

There are also the "hacked together when full blown ioc is either unneeded or cant be used for some managerial design decision".People should realize that if they can't use one, the can always roll something simple to meet their needs.Like when you want to have zero-dependency (but BCL).

I sincerely hop Unity is better than ObjectBuilder but why on earth would I choose it over Castle.

These guys should be ground breaking or Microsoft should be hiring some talent to set the standard.

I use open source because the tools are better. End of storty. If the MS tools were better I would switch instantly.

Paul Cowan

Friday, 14 March 2008 08:29:25 UTC

I didn't use ObjectBuilder a lot, but seems to me that it's not a full-blown IoC.From my experience with it, it is "just" a DI framework, without the container.Good enough that now the PnP build Unity :)

PS: context variables and transient (container-scoped components) represent common usage scenario in autofac. Resolving some transient component (registered in root container) in child container (with any depth of nesting) will fill in the dependencies from this child container first.

Thanks for mentioning Ninject in such great company. There'll be some new features coming in the near future now that I've been able to scrounge some more time to work on it. Hopefully some better documentation too. :)

Are there any IoC frameworks out there for the Compact Framework? I have spent some time looking but havn't found anything yet. This seems to leave two options, roll my own from scratch, or pick one of the existing IoC frameworks out there and try to port it over. I would love to hear opinions on which framework might be the easiest to port.

I wrote a simple (and free) service locator for DI that is described at here, and the code is available for download. It's a relatively simple implementation, but if you're new to new to DI/IoC, you might be interested in seeing how it works.

Hey Scott, I look forward to see what kind of conclusions you come to. I am still meaning to play around w/ some of the other frameworks you mention, however, StructureMap is my current IoC framework of choice. I wrote a Simple Dependency Injection with StructureMap tutorial to hopefully help anyone who has not used it before. Regards!

I didn't work on the original ObjectBuilder; that was Scott, Brad, & Peter Provost. Unity is being built on top of OB; OB : Unity <-> Castle Microkernel : Castle Windsor. Also, Unity isn't quite released yet; we've got a week or so to go to 1.0.

@Adam Salvo:

You might want to check out the Mobile Client Software Factory at http://msdn2.microsoft.com/en-us/library/aa480471.aspx. The main reason is that the team did a lot around DI with objectbuilder on the mobile platform. They discovered that reflection is HELLISHLY expensive on WinCE, so they wrote a preprocessing tool called obgen that does the reflection required at compile time, and generates the code to assemble objects. Might be worth checking out.

I have an open source framework called SharpCore hosted on CodePlex that has an IoC implementation. It was written mainly as an exercise in understanding what makes them work; plus, it was a lot of fun to write!

<plug type="shameless">I just published an article on DI/IoC in the March 2008 issue of MSDN Magazine. The advanced examples use Windsor, but the point of the article is to explain the underlying concepts. So if you are wondering why DI/IoC matters, go read Loosen Up: Tame Your Software Dependencies for More Flexible Apps. In the article, I roll a simple DIY IoC in about the same number of lines as Oren's or Ken's. Just another example of how you can easily use DI/IoC concepts in your application even if you can't use a full-fledged IoC framework for political reasons.</plug>

Looking over this 'complete' list reminds me of Ayende's concern that DI containers are too often evaluated only on the merits of their DI capabilities - I take my hat off to any reviewer who can achieve even that much in such a broad field of libraries!

He's right, of course - the more interesting features usually only present themselves once you're into the thick of building an application. I don't think they're really make-or-break though.

My advice to anyone reading this and wondering where to start is not to focus too much on the libraries - dependency injection is a powerful coding style that will make a much bigger impact on the architecture of an application than the particular container will. Once you've mastered dependency injection itself you'll have a much clearer idea of the features you want from a container.

Interesting list, looking forward to hearing some more along this theme :)