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.
In the IDemo interface file, register one method. Please find the below figure for reference in which I have registered the Display method.
Interface Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace APIProject.Services
- {
- public interface IDemo
- {
- string Display();
-
- }
- }
In DemoServices Class, implement the IDemo Interface. Please see the below figure.
DemoServices Class Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace APIProject.Services
- {
- public class DemoServices : IDemo
- {
- public string Display()
- {
- string message = "Hello C# Corner";
- return message;
- }
- }
- }
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.
- 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.
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.
- using APIProject.Services;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
-
- namespace APIProject.Controllers
- {
- public class DemoController : ApiController
- {
-
- private readonly IDemo _demoService;
- public DemoController() : this(new DemoServices())
- {
- }
- public DemoController(IDemo service)
- {
- _demoService = service;
- }
-
- [HttpGet]
- [Route("api/display")]
- public string Displaymethod()
- {
- string result = _demoService.Display();
- return result;
- }
- }
- }
Let's test the API through swagger. Please check the below figure for API response.
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.