Implement Dependency Injection in Web API With Simple Injector

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 a great idea but we will reduce tight coupling between the software components.

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

We can develop DI in 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 start coding

Step 1

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

Step 2

Select empty template and click OK and your solution will be ready.

Template

Step 3

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

solution

In the folder BusinessLayer, make a class, name it as SchoolBusinessLayer.cs and an interface ISchoolBusinessLayer.cs.

Similarly, in DataAccessLayer, make a class, name it as SchoolDataAccessLayer.cs and an 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 DataAccess Layer, I am just going to return an object school with the data. Our goal is to develop dependency injection.

In the model, I made a class file School.cs and declared some property.

Class

Step 5

Declare a method in ISchoolDataAccessLayer.cs, as shown below:

method

Step 6 Implement ISchoolDataAccessLayer.cs method in SchoolDataAccessLayer.cs, as shown below:

method

Step 7 In ISchoolBusinessLayer.cs, declare a method GetSchoolInformation, as shown below:

method

Step 8 Implement this method in SchoolBusinessLayer.cs, but this method gets data from DataAccessLayer,

For this, in constructor of SchoolBusinessLayer inject ISchoolDataAccessLayer.

Constructor

We are not directly dependent upon SchoolDataAccessLayer.cs (This technique is used to make an Application loosely coupled.)

Step 9

Make a SchoolController and in SchoolController constructor, implement ISchoolBusinessLayer, with the help of the injection.

code

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

Let’s run the project. When I run this project, it shows an error.

error

To solve this problem, I used simple injector.

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

It is use to develop an Application loosely couple.

For this we need to use Simple Injector.

Step 10 I install Simple Injector in my Project using

Install-Package SimpleInjector.Integration.WebApi -Version 3.1.5

Package
After successful installation in References two DLL's added,
  1. SimpleInjector.dll
  2. 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 Simple Injector and,?

And In Container Register Interface and there respected class,

And at the end verify all.

code
Step 12

I make a class SimpleInjectorDependencyResolver and call it from after container.Verify();

code

Step 13 Now run the project and you will find the output.

out put