Tutorial :Which Dependency Injection technique should I use in dotnet?


[Revision 5/14/09: I need this to target Dotnet 2.0 because of my deployment scenario]

I am new to Dependency Injection & IoC.

I see that there are a plethora of containers and libraries to help the process along.

What are the pros and cons of using these libraries. What is your favorite (dotnet) container and why?

What about rolling my own container for Dependency Injection?


Peter Stephens


If you need only Dependency Injection probably AutoFac will be the best option, since it's small, quite simple and logical. It should not be to hard to grok.

Once you get that, and you feel the need for more mature product (that can do other things than simply DI) I'd suggest taking a look at Castle Windsor, which is pretty powerful out of the box, very well designed, and customizable. If you don't like that, I've heard good things about StrcutureMap.




I use an IOC tool called StructureMap : Structure Map I found the tool to be very useful and easy to implement.


After some more research: Ninject seems to be a useful alternative. And it supports Dotnet 2.0. And it doesn't require massive XML configuration.

Still trying to figure out Best Practices for having a different DI configuration for the Production and Unit Testing environments.


Found another thread on this topic here


I like MEF, mainly because it's going to be very well supported in .NET 4.0 (since MS has announced that they're using it for the base of DI in Visual Studio 2010).

It's also quite simple to use, especially for DI. Handling DI and Add-In scenarios is the main goal of that framework, so it's very clean and simple in those cases.

It works for IoC as well, but that isn't its primary goal.


You can use Common Service Locator in your code, where it needs to call the container explicitly. This way if you're unhappy with the chosen framework you can switch it by pluging in the new one.


Spring.NET. If you only want to use the DI component of Spring, that's all you need to use (and you only need to reference those DLLs). Then later if you want to use the unit testing stuff, or the Quartz stuff, or the NHibernate stuff, you can add those DLLs too - it's built in a modular way. And all the different parts have a consistency about them because they're from the same framework. I think it's a really good framework that complements .NET very nicely.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »