What Is Ninject

There are several Inversion of Control (IoC) containers for .NET to pick from (such as Castle Windsor, Structure Map and Microsoft Unity, just to name just a few). Ninject is one of the newest entries in the arena, but it’s now sufficiently stable at version 2.0.

Ninject tries to focus on “simplicity and ease of use”, removing features that are not deemed necessary (to the point that XML configuration is not offered out-of-the box).

In this and following posts we’ll explore some example of how to use Ninject. I assume that you are somehow familiar with the basic concepts of Inversion of Control and Dependency Injection; if that’s not the case, you should consider having a look at this wikipedia entry (better yet, take some time and read Martin Fowler famous post on the subject).

Setup

Installing Ninject is quite easy: you simply download the pre-built DLLs it from here. Since Ninject is open source, you can also get the sources from github and build it on your own.

On my Windows machine, I have copied the DLLs on C:\Ninject.

UPDATE: nowadays you would obviosly use NuGet.

Getting your Feet Wet With Ninject

Once you have Ninject DLLs somewhere on your hard-drive, in order to get started you only need to reference them (typically you only need to reference NInject.dll ). As we said, no XML configuration is required.

Let’s see a few basic examples (the complete source code with unit tests I present in this series are available on github).

"Hello, Ninject"

Suppose we have a fairly simple service to calculate the taxes for a given amount, defined in an ITaxCalculator interface:

decimal CalculateTax ( decimal gross )

and a trivial implementation TaxCalculator as follows:

public class TaxCalculator : ITaxCalculator { private readonly decimal _rate ; public TaxCalculator ( decimal rate ) { _rate = rate ; } public decimal CalculateTax ( decimal amount ) { return Math . Round ( _rate * amount , 2 ); } }

Now, one or more classes might need to use an ITaxCalculator implementation to fulfill their responsibility (such as calculating the total price for a shopping cart). We can say that an implementation of ITaxCalculator is a dependency to them.

Like many IoC containers, Ninject uses a central object (which it calls the kernel) to provide concrete implementations of dependencies at run-time. The Standard Kernel is the default implementation of such an object. Let's see it in action:

using ( IKernel kernel = new StandardKernel ()) { kernel . Bind < ITaxCalculator >() . To < TaxCalculator >() . WithConstructorArgument ( "rate" , . 2 M ); var tc = kernel . Get < ITaxCalculator >(); Assert . Equal ( 20 M , tc . CalculateTax ( 100 M )); }

As you can see, through a fluent interface we are instructing the kernel how to bind (resolve) requests for a ITaxCalculator to a TaxCalculator class (a concrete implementation), passing to its constructor a given tax rate (20% in this case).

The example continues showing how a client can retrieve an implementation of the service through the kernel (via the Get() method) and use it.

Some Magic

You might argue that the little example above is far from impressing. So let's now see Ninject performing something more clever.

Suppose we have a Sale class modeling a ongoing transaction on a ecommerce site. Such a class in our example depends on a ITaxCalculator to compute the final price of the shopping cart.

public class Sale { private readonly ITaxCalculator taxCalculator ; public Sale ( ITaxCalculator taxCalculator ) { this . taxCalculator = taxCalculator ; } // more stuff.... public decimal GetTotal () { // use the tax calculator to calculate the total } }

We might create the sale in the obvious way, based on the preceding example:

kernel . Bind < ITaxCalculator >() . To < TaxCalculator >() . WithConstructorArgument ( "rate" , . 2 M ); var sale = new Sale ( kernel . Get < ITaxCalculator >());>

More interestingly, it's possible to let Ninject to find out how a Sale should be built based on the binding information it has received:

kernel . Bind < ITaxCalculator >() . To < TaxCalculator >() . WithConstructorArgument ( "rate" , . 2 M ); var sale = kernel . Get < Sale >();

Ninject is smart enough to build a Sale class for us taking care of fulfilling the dependencies behind the scenes. This an example of autowiring, a most convenient feature of many IoC containers.

Go to Part 2