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.

Step 2

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

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 { get; set; }
  10. public DateTime Date { get; set; }
  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 { get; set; }
  10. public int Pagecount { get; set; }
  11. public IEnumerable<T> Rslt { get; set; }
  12. }
  13. }

Step 3

Add a new Folder to the project.

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. public ResultModel<ResponseModel> Get()
  23. {
  24. return this.Get(1);
  25. }
  26. public ResultModel<ResponseModel> Get(int No_page)
  27. {
  28. var Gap_No = (No_page - 1) * PageSize;
  29. return new ResultModel<ResponseModel>
  30. {
  31. Full_count = intercollect.Count(),
  32. Pagecount = No_page,
  33. Rslt = intercollect.Skip(Gap_No).Take(PageSize)
  34. };
  35. }
  36. }
  37. }

Step 4

Create a Controller "ResponseController".

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==null) throw 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:

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