Agile Zone is brought to you in partnership with:

Mitch Pronschinske is the Lead Research Analyst at DZone. Researching and compiling content for DZone's research guides is his primary job. He likes to make his own ringtones, watches cartoons/anime, enjoys card and board games, and plays the accordion. Mitch is a DZone Zone Leader and has posted 2576 posts at DZone. You can read more from them at their website. View Full User Profile

Ninject 2 Dependency Injection Reaches GA

02.26.2010
| 6625 views |
  • submit to reddit
After a year in beta, Ninject 2 has finally gone GA.  Nate Kohari blogged about the free tool's release yesterday and explained why the delays took so long.  Ninject is especially known for its fluent interface, which sets it apart from many other DI frameworks.  Ninject 2 is a rewrite of Ninject 1 from the ground up.  The 2.0 version features its own Inversion-of-Control (IoC) container, multi-injection, and a new scoping system.

Why DI?
Ninject is a Dependency Injection (DI) framework for .NET development.  It splits applications into a collection of loosely-coupled, highly-cohesive pieces, and then glues them back together.  If you inject dependencies without a DI framework, you will generate arrow code all over your application telling classes how to build their dependencies:
    public Contact()
: this(new DataGateWay())
{
}
A tool like Ninject condenses the arrow code into one location so dependencies are easier to change:
internal class ProductionModule : StandardModule
{
public override void Load()
{
Bind<IDataGateway>().To<DataGateWay>();
}
}
DI frameworks also make code testing easier by mocking the interfaces that are needed for a particular code block.  Functionalities can easily be swapped without breaking other parts of the code.

Ninject 1 vs. Ninject 2
Ninject 2 relies on LINQ, so it doesn't support .NET 2.0 or lower.  It's better if you use Ninject 1 for anything lower than .NET 3.5.  A major change in Ninject 2 is the new scoping system that uses the garbage collector to regain instances instead of binding behaviors.  Any POCO can become a scoping object.  You can find the syntax changes for defining lifecycle on Kohari's blog.  There are also new constructor selection semantics.  In Ninject 2, constructors with an [Inject] attribute are used, but with multiple constructors having [Inject] you'll get a NotSupportedException.  If you don't put any [Inject] attributes on your constructors, Ninject 2 will choose one that contains the most resolvable parameters.  With no constructors defined, Ninject 2 will just select the default parameterless constructor.  Behavior attributes and field injection have been removed from Ninject 2.

Other new features in Ninject 2 include:

  • Multi-injection: The kernel for Ninject 2 has GetAll<T>() methods, and supports multiple target injections with types IEnumerable<T>, List<T>, and arrays of T.  Because of multi-injection support, Ninject 2 also has full support for the Common Service Locator.
  • Constrained resolution: From the point of injection, constraining predicates are now able to flow into a resolution request.
  • Mono Support: Full support for Mono's version of the CLR.
  • Optional Modules: Modules are no longer required to register bindings directly on the kernel.
  • Automatic Module Scanning: Directories can be scanned for module-containing assemblies and then load them into the kernel.
  • IoC:  Ninject 2 has its own Inversion of Control container, making core extension simpler.


What took so long?

Nate Kohari explained that the year long delay between Ninject 2 beta and GA was caused by his "crazy idea to launch a startup."  His new company, AgileZen, has built an application for Kanban management.  With the business consuming most of his time Kohari's initial GA date in March 2009 came and went.  Thanks to the help of a long time contributor, Ian Davis (who is now a co-maintainer), the final 2.0 version was able to finally be launched. Kohari said that no further work will be done on Ninject 1 except for critical bug fixes.

You can get Ninject 2 from the repository on GitHub or you can download the binaries from the redesigned Ninject site.