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).
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).
Suppose we have a fairly simple service to calculate the taxes for a given amount,
defined in an
and a trivial implementation
TaxCalculator as follows:
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:
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
Get() method) and use it.
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
ITaxCalculatorto compute the final price of the shopping cart.
We might create the sale in the obvious way, based on the preceding example:
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:
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