Introduction
Dependency injection (DI) is a design pattern that you can use to apply the loose coupling principle. In software programming generally, it is good practice to make your code agnostic and is not tightly coupled with other code. DI is a form of inversion of control where you separate the classes/objects construction from its usage. As it is illustrated in the figure below, In traditional coding style if ClassA depends on ClassB, inside ClassA you define an object from ClassB and use it, but in case you need to change the behavior of ClassB, you have to do changes in ClassA to reflect the changes of ClassB inside ClassA. Using DI, you can create an interface of ClassB (IClassB) then inject IClassB in the ClassA constructor and so you can change the implementation of ClassB freely without changing ClassA.
Tradition vs dependency injection
Dependency injection in .net core
In asp.net core, the dependency injection (DI) and inversion of control (IoC) are implemented by default within the framework.
Working without dependency injection
Without dependency injection, your code will be tight coupled and it will be very hard to write unit tests also your code will not accept business changes easily.
Demo
Let’s create a .net core project using visual studio 2022 to apply the DI.
- In the beginning, open visual studio 2019 or recent, in the search text box add the template name “core web API”
Select project template details with creating API project
- Create two class library projects with the names Namespace.Core and Namespace.Application.
- The core project will have all interfaces that represent the project logic
- The application project will contain all the business implementations
Create class library projects
Inside the core project, create a folder with the name ‘Interfaces’, then create our first interface with the name ICustomerService. The ICustomerService interface will contain the abstract logic which is related to customer business. Inside ICustomerService interface, create a function with the name GetName to get the customer name.
namespace EatTheCode.DependencyInjection.Core.Interfaces { public interface ICustomerService { string GetName(int id); } }
Inside the application project, create a folder with the name ‘Services’, then create a class with the name CustomerService. The CustomerService class will contain the actual logic implementation and this class will implement the interface ICustomerService, so we have to add the core project as a reference inside The application project.
using EatTheCode.DependencyInjection.Core.Interfaces; namespace EatTheCode.DependencyInjection.Services.Business { internal class CustomerService: ICustomerService { public string GetName(int id) { return $"Customer with id: {id}"; } } }
In the Program.cs, add the following code
builder.Services.AddScoped<ICustomerService, CustomerService>();
Note: AddScoped: if you will use the scoped lifetime, with each client request, you will create an instance of the service. This is useful since it allows you to share the same service instance for the duration of an HTTP request processing.
Now create a controller with the name “CustomerController”, as the following code,
using EatTheCode.DependencyInjection.Core.Interfaces; using Microsoft.AspNetCore.Mvc; namespace EatTheCode.DependencyInjection.Api.Controllers { [Route("api/[controller]")] [ApiController] public class CustomerController : ControllerBase { ICustomerService _customer { get; } public CustomerController(ICustomerService customer) { _customer = customer; } [HttpGet] public ActionResult<string> GetName(int id) { string customerName = _customer.GetName(id); return customerName; } } }
Finally using postman or swagger, you can find the results below,
.net core dependency injection demo result
Conclusion
Dependency injection allows you to apply the loosely coupling principle easily. With DI, you can create tests easily and your system can easily accept business changes. In this blog, I illustrated the dependency injection and the work without and with DI. I create a step-by-step solution in asp.net core to apply DI with showing its benefits.
You can find the project source code at github
Enjoy…!!!
I can help you to build such as software tools/snippets, you contact me from here