Implementation of Dependency Injection in Web API

Introduction

In this article, I will show you a simple implementation of Dependency Injection in the Web API. Here we create an application that implements Dependency Injection.

Step 1

Create a Web API application.

  • Start Visual Studio 2012.
  • From the start window Select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click on the "Ok" button.

    Select MVC4 Web Application

  • From the "MVC4 Project" window select "Web API".

    Select Web API

  • Click on the "OK" button.

Step 2

Create Two Model classes first is "ResponseModel" and another as "ResultModel".

  • In the "Solution Explorer".
  • Right-click on the Model Folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select "Class".
    Add Model Class

Add the following code in the "ResponseModel.cs".

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace DepeninjecAPI.Models  
  6. {  
  7.     public class ResponseModel  
  8.     {  
  9.         public int ID { getset; }  
  10.         public DateTime Date { getset; }  
  11.     }  
  12. }  

 

Add the following code in "ResultModel.cs" class.

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace DepeninjecAPI.Models  
  6. {  
  7.     public class ResultModel<T>  
  8.     {  
  9.         public int Full_count { getset; }  
  10.         public int Pagecount { getset; }  
  11.         public IEnumerable<T> Rslt { getset; }  
  12.     }  
  13. }  

 

Step 3

Add a new Folder to the project.

  • In the "Solution Explorer".
  • Right-click on the Project.
  • Select "Add" -> "New Folder"and change the name of it to "Collect".

    Add New Folder
    Add a new interface in this folder.
  • Right-click on the "Collect" folder.
  • Select "Add" -> "New Items".
  • Select "installed" -> "Visual C#" and select "Interface".
    Add Interface

Add the following code in this interface:

  1. using DepeninjecAPI.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. namespace DepeninjecAPI.Collect  
  7. {  
  8.     public interface IRedingCollect  
  9.     {  
  10.         void Save(ResponseModel read);  
  11.         ResultModel<ResponseModel> Get();  
  12.         ResultModel<ResponseModel> Get(int No_page);  
  13.     }  
  14.     public class ReadingCollect :IRedingCollect  
  15.     {  
  16.         private readonly IList<ResponseModel> intercollect = new List<ResponseModel>();  
  17.         public const int PageSize = 5;  
  18.         public void Save(ResponseModel read)  
  19.         {  
  20.             intercollect.Add(new ResponseModel { ID = read.ID, Date = read.Date });  
  21.   
  22.         }  
  23.         public ResultModel<ResponseModel> Get()  
  24.         {  
  25.           return this.Get(1);  
  26.         }  
  27.         public ResultModel<ResponseModel> Get(int No_page)  
  28.         {  
  29.             var Gap_No = (No_page - 1) * PageSize;  
  30.             return new ResultModel<ResponseModel>  
  31.             {  
  32.                 Full_count = intercollect.Count(),  
  33.                 Pagecount = No_page,  
  34.                 Rslt = intercollect.Skip(Gap_No).Take(PageSize)  
  35.             };  
  36.         }  
  37.     }  
  38. }   

Step 4

Create a Controller "ResponseController".

  • In the "Solution Explorer".
  • Right-click on the Controller folder.
  • Select "Add" -> "Controller".

    Add Controller

  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using DepeninjecAPI.Models;  
  8. using DepeninjecAPI.Collect;  
  9. namespace DepeninjecAPI.Controllers  
  10. {  
  11.     public class ResponseController : ApiController;'  
  12.     {  
  13.         private readonly IRedingCollect collect;  
  14.         public ResponseController(IRedingCollect col)  
  15.         {  
  16.             collect = col;  
  17.         }  
  18.         public void Post(ResponseModel read)  
  19.         {  
  20.             if(read==nullthrow new HttpResponseException(HttpStatusCode.BadRequest);  
  21.             collect.Save(read);  
  22.         }  
  23.         public ResultModel<ResponseModel> Get()  
  24.         {  
  25.             return Get(1);  
  26.         }  
  27.         public ResultModel<ResponseModel> Get(int No_page)  
  28.         {  
  29.             return collect.Get(No_page);  
  30.         }  
  31.     }  
  32. }   

Step 5

Now we add a class in the "App_start" folder:

  • In the "Solution Explorer"

  • Right-click on the "App_Start" folder.

  • Select "Add" -> "Class".

Add the following code:

 

  1. using DepeninjecAPI.Collect;  
  2. using DepeninjecAPI.Controllers;  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Http.Dependencies;  
  8. namespace DepeninjecAPI.App_Start  
  9. {  
  10.     public class SolveController:IDependencyResolver  
  11.     {  
  12.         private static readonly IRedingCollect readcollect = new ReadingCollect();  
  13.         public object GetService(Type type)  
  14.         {  
  15.             return type == typeof(ResponseController) ? new ResponseController(readcollect) : null;  
  16.         }  
  17.         public IEnumerable<object> GetServices(Type type)  
  18.         {  
  19.             return new List<object>();  
  20.         }  
  21.         public IDependencyScope BeginScope()  
  22.         {  
  23.            return this;  
  24.         }  
  25.         public void Dispose()  
  26.         {  
  27.         }  
  28.     }  
  29. }  

 

In the "WebApi.config" file add the following line of code:

config.DependencyResolver = new SolveController(); 

Step 6

Execute the application and change the URL to "http://localhost:43200/api/Response".Then the output will be as in the following:

 Output


Similar Articles