As Martin Fowler mentions it in his article, the basic idea of the Dependency Injection (DI) is to have a separate object, that populates a field in another class with an appropriate implementation of a specific interface. DI can be applied in three different ways: Constructor Injection, Setter Injection, and Interface Injection. A container I’ve used on several projects is the Pico Container.
Applied to the figure below, we will take Martin’s idea to say that: The basic idea of the Dependency Injection is to have a separate object, an assembler, that populates a field in the lister class with an appropriate implementation for the finder interface, resulting in a dependency diagram.
Let’s go over the 3 types of DI.
Constructor Injection: A container like Pico uses a constructor to decide how to inject an implementation into a class.
Setter Injection: We define in a specific class, a setting method for a service we want to inject in a class. Spring.NET makes a big use of this method.
Interface Injection: This technique defines and uses interfaces for the injection. Avalon users enjoy it.
public interface InjectFinder { void injectFinder(MovieFinder finder); }
This interface would be defined by whoever provides the MovieFinder interface. It needs to be implemented by any class that wants to use a finder, such as the lister.
class MovieLister implements InjectFinder
public void injectFinder(MovieFinder finder) { this.finder = finder; }
Coming back to DI frameworks, I have found the following on StackOverflow and it does a pretty good description of those. The question was: Which .NET DI framework are worth looking into?
And here is the answer.
It depends on what you are looking for, as they each have their pros and cons.
Spring.NET
is the most mature as it comes out of Spring from the Java world. Spring has a very rich set of framework libraries that extend it to support Web, Windows, etc.Castle Windsor
is one of the most widely used in the .NET platform and has the largest ecosystem, is highly configurable / extensible, has custom lifetime management, AOP support, has inherent NHibernate support and is an all around awesome container. Windsor is part of an entire stack which includes Monorail, Active Record, etc. NHibernate itself builds on top of Windsor.Structure Map
has very rich and fine grained configuration through an internal DSL.Autofac
is an IoC container of the new age with all of it’s inherent functional programming support. It also takes a different approach on managing lifetime than the others. Autofac is still very new, but it pushes the bar on what is possible with IoC.Ninject
I have heard is more bare bones with a less is more approach (heard not experienced).- The biggest discriminator of
Unity
is: it’s from and supported by Microsoft (p&p). Unity has very good performance, and great documentation. It is also highly configurable. It doesn’t have all the bells and whistles of say Castle / Structure Map.
So in summary, it really depends on what is important to you. I would agree with others on going and evaluating and seeing which one fits. The nice thing is you have a nice selection of donuts rather than just having to have a jelly one.
Sources
http://stackoverflow.com/questions/21288/which-net-dependency-injection-frameworks-are-worth-looking-into
http://www.martinfowler.com/articles/injection.html