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()A tool like Ninject condenses the arrow code into one location so dependencies are easier to change:
: this(new DataGateWay())
internal class ProductionModule : StandardModuleDI 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.
public override void Load()
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.