Implement Dependency Injection In Web API

First, we have to know what dependency injection is. DI is a design pattern and with the help of this, we will develop a loosely coupled code.

This is the great idea, through which we will reduce tight coupling between software components.

The main Idea of DI is to develop a project with maintainable code.

We can develop DI with different ways,

  1. Constructor Injection
  2. Property Injection
  3. Method Injection

In this article, we will develop DI with the help of constructor injection.

Now let's start coding,

Step 1

Open Visual Studio => New Project => Web => Visual Studio 2012 => ASP.Net MVC 4 Web Application, give a nice name to your project and click OK.

Step 2

Select Empty Template => OK and your solution is ready.



Step 3

I’m not going to make this solution too heavy, for this, I made two foldes in this solution, and gave the name BusinessLayer and DataAccessLayer.



In the folder BusinessLayer, make a class- SchoolBusinessLayer.cs and one interface- ISchoolBusinessLayer.cs
SimilarinDataAccessLayer. Make a class- SchoolDataAccessLayer.cs and one interface- ISchoolDataAccesslayer.cs.

Step 4

Basically, DataAccesslayer is used to communicate with the database but, in this article, I’m not going to use any kind of database communication.

From Data access Layer, I am just going to return an object school with the data. Our goal is to develop Dependency Injection.

In Model, I will make a class file School.cs and declare some property.



Step 5

Declare a method in ISchoolDataAccessLayer.cs.



Step 6

ImplementISchoolDataAccessLayer.cs method in SchoolDataAccessLayer.cs.


Step 7

In ISchoolBusinessLayer.cs, declare a method GetSchoolInformation.



Step 8

Implement this method in SchoolBusinessLayer.cs. This method gets data from DataAccessLayer.

For this, in Constructor of SchoolBusinessLayer, inject ISchoolDataAccessLayer.



Hence, we are not directly dependent upon SchoolDataAccessLayer.cs (This technique is used to make an Application Loosely Couple).

Step 9

Make a SchoolController and in SchoolController Constructor,  implement ISchoolBusinessLayer, with the help of an Injection.



Throughout the project development, we never created any object. We communicated with one layer and another with the help of an interface.

Let's run the project. 



I used Simple Injector.

Now, we have to discuss about Simple Injector. Simple Injector is an easy, flexible and fast dependency injection.

It is used to develop an Application, which is loosely coupled.

For this, we need to use Simple Injector

Step 10

I install Simple Injector in my Project using

Install-Package Simple Injector.Integration.WebApi -Version 3.1.5



After successful installation, two DLL's are added, as show below:

SimpleInjector.dll
SimpleInjector.Extensions.ExecutionContextScoping.dll


Now we have to do some code for simple Injector.

Step 11

In Gloabal.asax.cs, make a container with the help of a Simple Injector, Container Register Interface and there is a respected class and at the end, verify all.



Step 12

I made a class SimpleInjectorDependencyResolver and call it from After container.Verify();



Step 13

Now, run the project and you will find the output.

Up Next
    Ebook Download
    View all
    Learn
    View all