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
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.
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.
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.
Adding Reference of Ninject Framework.
Now I had added a reference for the Ninject Framework to my application.
See in the following snapshot.
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.
Adding DL class
After adding BL.cs I have added the DL class to the application (Data Access Layer).
See in the following snapshot.
Adding Interface
For decoupling both classes I have added an interface with the name IProduct.
See in the following snapshot.
In the interface, I have declared a method with the name Insertdata().
See in the following snapshot.
Now the DL (Data Access Layer) will implement the interface IProduct.
See it in the following snapshot.
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.
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.
After inheriting NinjectModule and Configuring Load() now we are moving towards Program.cs.
In program class
Step 1: We are creating an instance of Class StandardKernel.
Step 2: Then we will load the Kernel.
Step 3: Get the instance of the specific service that we want to inject.
Step 4: Then inject the dependency.
Here IProduct will get an instance of DL that I will pass to BL.
Step 5: Finally calling the method of DL.
See in the following snapshot containing the entire Program.cs.
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.
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.
See in the following snapshot, it is calling a method of a DA (Data Access) layer.
See in the following snapshot it has called the DA (Data Access) layer.
Final output
It has finally injected dependency.