Dependency Injection Using Ninject in .NET

Ninject

I recently wrote an article on Dependency Injection using the Microsoft Unity framework. I was trying other open-source tools for Dependency Injection and found Ninject was a good and open-source tool. This article provides a complete view of Dependency Injection with Ninject step-by-step. I like to provide simple code examples so 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 for 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, you then you will find a similar example such that you can easily understand how to use Ninject. If you want to learn the Microsoft Unity framework, 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.
  • I am running and debugging the application.
  • Final output

Step 1. Download Ninject:: If you want to download Ninject, you can download it from this link. You can also download it from the NuGet packages manager. To download Ninject 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. In the left search text add Ninject and select the first option Ninject (IoC container for . Net) for this sample we need this package. You can see this 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 (which is quite widely used). See the following snapshot.

NinjectConsole

Adding Reference of Ninject Framework. Now I have added a reference for the Ninject Framework to my application. See the following snapshot.

Ninject Framework

After adding the reference for the Ninject Framework to my application. Now I will add some classes to the application.

Adding BL class

I have added BL.cs (a Business Logic Layer) class to the application. See 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 the following snapshot.

Data Access Layer

Adding Interface

For decoupling both classes I have added an interface with the name IProduct. See the following snapshot.

Adding Interface

In the interface, I have declared a method with the name Insertdata(). See 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 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 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 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 byproduct which 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 the following snapshot it is called the DA (Data Access) layer.

Called the DA

Final output

It has finally injected dependency.

Dependency injection

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.


Similar Articles