[Design Patterns – Tools] Ninject (DI)

Ninject is one of the most popular DI (Dependency Injection) frameworks. It is robust and easy to use; moreover it is free. You can always use DI pattern for your application but Ninject helps you integrate DI into your application.

 

1. Ninject and DI

DI helps you create a loose-coupled code. Rather than creating a concrete object all over the place, you rely on interfaces.

  • Define a varaible as the interface type (not a concrete class type)
  • Provide a constructor to set an object to a variable

But where you are going to create an object and pass it to a consumer class? DI frameworks such as Ninject  help you solve this problem.

Bind<IService>().To<ServiceImpl>();

This code is the main idea of Ninject. By registering this code in an application, whenever an instance of “IService” is requested an instance of “ServiceImpl” is created and returned.

 

2. DI by Hand

Look at the following sample code as a starting point. It is already refactored using DI.

public interface IMedia
{
  void Play();
}

public class Video : IMedia
{
  public void Play()
  {
    Console.WriteLine("Playing Video...");
  }
}

public class Audio : IMedia
{
  public void Play()
  {
    Console.WriteLine("Playing Audio...");
  }
}

public class MediaPlayer
{
  private IMedia media;

  public MediaPlayer(IMedia media)
  {
    this.media = media;
  }
  public void Play()
  {
    media.Play();
  }
}

public static class MediaPlayerTest
{
  public static void Test()
  {
    MediaPlayer videoplayer = new MediaPlayer(new Video());
    MediaPlayer audioplayer = new MediaPlayer(new Audio());

    videoplayer.Play();
    audioplayer.Play();
  }
}

 

3. Installing Ninject

You can download the package from the Ninject homepage. But it is easy to install Ninject in VS through “NuGet”.

  • Right click the project and select the “Manage NuGet Packages…” menu
  • Select the “Browse” tab and search “Ninject
  • Click the “Install” Button

You can find the “Ninject” assembly in the “References” tab in the solution explorer.

 

4. Kernel

The starting point of NInject is a “kernel“. It orchestrates all features. So you need to get an instance of it first.

The behaviors of a kernel is defined in the “IKernel” interface and the “StandardKernel” class is the basic implementation of it.

IKernel kernel = new StandardKernel();

 

5. Type Bindings

The most common task of Ninject is map the interface type with a concrete class type. It is called Type binding.

IKernel kernel = new StandardKernel();
kernel.Bind<IMedia>().To<Video>();

This type binding means that whenever Ninject encounters a dependency on “IMedia“, it will resolve an instance of “Video” and inject it.

 

6. Getting an Object

To get an object, you need to use “IKernel.Get()” method.

IKernel kernel = new StandardKernel();
var vedio = kernel.Get<IMedia>();

 

7. Contextual Bindings – Named Bindings

What if you want to bind mutliple classes to a single interface? How does Ninject figure out which class should be instantiated?

The solution is to give a specific name to each binding.

IKernel kernel = new StandardKernel();
kernel.Bind()<IMedia>.To<Video>().Named("Video");
kernel.Bind()<IMedia>.To<Audio>().Named("Audio");

When you get an object, you need to pass the name.

var vedio= kernel.Get<IMedia>("Video");
var vedio= kernel.Get<IMedia>("Audio");

 

8. DI by Ninject

Let’s modify the code in the section 2.

public static class MediaPlayerTest
{
  public static void Test()
  {
    IKernel kernel = new StandardKernel();

    kernel.Bind<IMedia>().To<Video>().Named("Video");
    kernel.Bind<IMedia>().To<Audio>().Named("Audio");

    var videoplayer = new MediaPlayer(kernel.Get<IMedia>("Video"));
    var audioplayer = new MediaPlayer(kernel.Get<IMedia>("Audio"));

    videoplayer.Play();
    audioplayer.Play();
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s