Web API Using Poor Man's Dependency Injection

Introduction

 
Dependency injection is basically the process of providing the objects that an object needs (its dependencies) instead of having it construct them itself. It's a very useful technique for testing.
 
You don't need any framework to have dependency injection. Instantiating and passing objects (dependencies) explicitly is just as good an injection as an injection by framework. Today, we are discussing in this blog about dependency injection and how to write APIs using the poor man's dependency injection.
 
Create one Web API Project. If you are a beginner, please refer to the below blog for creating a Web API solution.
Please add one folder to your API solution. Check the below figure for reference where I have added one folder and created one class file and interface inside that.
 
Web API Using Poor's Man Dependency Injection
 
In the IDemo interface file, register one method. Please find the below figure for reference in which I have registered the Display method.
 
Web API Using Poor's Man Dependency Injection
 
Interface Code Snippet
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace APIProject.Services  
  8. {  
  9.    public  interface IDemo  
  10.     {  
  11.         string Display();  
  12.           
  13.     }  
  14. }  
In DemoServices Class, implement the IDemo Interface. Please see the below figure.
 
Web API Using Poor's Man Dependency Injection
 
DemoServices Class Code Snippet
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace APIProject.Services  
  7. {  
  8.     public class DemoServices : IDemo  
  9.     {  
  10.         public string Display()  
  11.         {  
  12.             string message = "Hello C# Corner";  
  13.             return message;  
  14.         }  
  15.     }  
  16. }  
Please add one web API controller to your project. In that controller, let's implement Poor Man's Dependency Injection.
 
In your controller, first, declare one private variable for an interface.
  1. private readonly IDemo _demoService;  
Next, create default and parameterized constructor in your controller. Through constructor, we are creating an instance for our service class and injecting the all properties of service class to interface variable.
 
Web API Using Poor's Man Dependency Injection
 
When the default constructor is used to instantiate a controller, the demo service member points to a newely created instance of a default demo service class. A second constructor is available to manually inject any instance you like, at least for testability reasons. Likewise, you do the same for injecting the repository dependency into the demo service.
 
Controller Code Snippet
  1. using APIProject.Services;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8.   
  9. namespace APIProject.Controllers  
  10. {  
  11.     public class DemoController : ApiController  
  12.     {  
  13.   
  14.         private readonly IDemo _demoService;  
  15.         public DemoController() : this(new DemoServices())  
  16.         {  
  17.         }  
  18.         public DemoController(IDemo service)  
  19.         {  
  20.             _demoService = service;  
  21.         }  
  22.   
  23.         [HttpGet]  
  24.         [Route("api/display")]  
  25.         public string Displaymethod()  
  26.         {  
  27.             string result = _demoService.Display();  
  28.             return result;  
  29.         }  
  30.     }  
  31. }  
Let's test the API through swagger. Please check the below figure for API response.
 
Web API Using Poor's Man Dependency Injection
 

Summary

 
In this blog, we discussed how to implement the poor man's dependency injection in web API.

I hope you found it useful. Eat-> Code->Sleep->Repeat.