Dependency Injection Using Ninject in .NET

Ninject
 
I recently wrote an article on Dependency Injection using the Microsoft Unity framework. I was trying some other open-source tools for Dependency Injection and I found Ninject was a good tool and open source too.
 
This article provides a complete view of Dependency Injection with Ninject in a step-by-step process. I like to provide simple code examples that the user will find it easy to understand the concepts.
 
Ninject is open-source and hosted on GitHub.
 

Dependency Injection

 
Dependency Injection is defined as a design pattern that allows removing hard-coded dependencies from an application.
 
How to remove Dependency Injection
 
One way to remove a dependency is to use a framework such as Unity, Ninject, Windsor Castle, and many more that will help to remove the dependency.
 
Tool required
 
Visual Studio 2010 SP1 and above.
 
Ninject DLLs.
 
If you have read my article on Microsoft Unity then you will find a similar example here such that you can easily understand it to use Ninject.
 
If you want to learn the Microsoft Unity framework then refer to this link:
 
Agenda
  • Download Ninject
  • Creating a console application.
  • Adding Reference of Ninject Framework.
  • Adding BL class.
  • Adding DL class.
  • Adding Interface.
  • How to configure the Ninject Framework.
  • Running and debugging the application.
  • Final output
Step 1: Download Ninject
 
If you want to download Ninject then you can download it from this link.
 
You can also download it from the NuGet packages manager.
 
To download Ninject just right-click on the application and select Manage NuGet Packages as you can see in the following snapshot.
 
Manage NuGet Packages
 
After clicking on Manage NuGet Packages a new dialog will pop up. Inside that just in the left search text just add Ninject and the select first option Ninject (IoC container for .Net) for this sample we need this package.
 
You can see in the following snapshot.
 
select first option Ninject
 
Now let's start with creating the solution.
 
Creating a console application
 
I am creating a Console Application named NinjectConsole.
 
And I am showing Constructor Injection with the Ninject Framework (that is quite widely used). See in the following snapshot.
 
NinjectConsole
 
Adding Reference of Ninject Framework.
 
Now I had added a reference for the Ninject Framework to my application.
 
See in the following snapshot.
 
Ninject Framework
 
After adding the reference for the Ninject Framework to my application.
 
Now I will add some class to the application.
 
Adding BL class
 
I have added BL.cs (a Business Logic Layer) class to the application.
 
See in the following snapshot.
 
Business Logic Layer
 
Adding DL class
 
After adding BL.cs I have added the DL class to the application (Data Access Layer).
 
See in the following snapshot.
 
Data Access Layer
 
Adding Interface
 
For decoupling both classes I have added an interface with the name IProduct.
 
See in the following snapshot.
 
Adding Interface
 
In the interface, I have declared a method with the name Insertdata().
 
See in the following snapshot.
 
declared a method
 
Now the DL (Data Access Layer) will implement the interface IProduct.
 
See it in the following snapshot.
 
DL
 
After implementing the interface, now in the BL (Business Logic) layer, I am injecting a dependency from the Constructor.
 
See in the following snapshot.
 
 
How to configure Ninject
 
Note that in the Unity framework you can directly register a type and resolve the injected dependency.
 
To configure the Ninject Kernel we first need to create a Class with a unique name and inherit it from NinjectModule.
 
Ninject allows us to solve this problem by creating a module.
 
I have given the class name NinjectBindings.
 
See in the following snapshot.
 
NinjectBindings
 
After inheriting from NinjectModule now in the Load() method we need to declare the dependence of what we will inject.
 
The preceding code shows injecting DL using IProduct.
 
For example, you can change the Bindings class to use the DL wherever IProduct is used.
 
IProduct
 
After inheriting NinjectModule and Configuring Load() now we are moving towards Program.cs.
 
In program class
  1. We will create an instance of the class StandardKernel.
  2. Then we will load the Kernel.
  3. Get the Instance of a specific service.
  4. Then inject the Dependency.
  5. Finally, call the method of DL.
Step 1: We are creating an instance of Class StandardKernel.
 
StandardKernel
 
Step 2: Then we will load the Kernel.
 
load Kernel
 
Step 3: Get the instance of the specific service that we want to inject.
 
specific service
 
Step 4: Then inject the dependency.
 
Here IProduct will get an instance of DL that I will pass to BL.
 
get instance
 
Step 5: Finally calling the method of DL.
 
calling method
 
See in the following snapshot containing the entire Program.cs.
 
entire Program
 
Running and debugging application
 
Now just run the application.
 
I show below a snapshot of the running BL during debugging that clearly shows the DL injecting. See in the following snapshot the Data Access layer injecting using the constructor.
 
You can see that I pinned the objIproduct that is clearly showing the DL is being injected.
 
objIproduct
 
See in the following snapshot a method of BL has been called.
 
Here I am passing an object of DL that you can see in Pin _objIpro.
 
passing object of DL
 
See in the following snapshot, it is calling a method of a DA (Data Access) layer.
 
calling a method of a DA
 
See in the following snapshot it has called the DA (Data Access) layer.
 
called the DA
 
Final output
 
It has finally injected dependency.
 
Final output
 

Conclusion

 
Dependency Injection is difficult if you are new to this.
 
Using Dependency Injection you can write cleaner code that is easy to test.
 
I have given a simple example to make it easy to understand the concepts.


Recommended Free Ebook
Similar Articles