Dependency Injection in ASP.NET Core


When programming the term dependency comes up quite frequently. All a dependency is is an object that another object relies upon to get it's job done. Dependency Injection (DI) is a design pattern used to implement Inversion of Control between classes and their dependencies. Inversion of Control simply means that the control of object creation and management is transferred from the programmer and the application code to a container or framework. This process allows for better modularity, testability and maintainability of code. ASP.NET Core provides a built-in container for handling dependency injection which was added in 2016. This makes it easier to handle dependencies in applications.

How to use Dependency Injection?

Dependency injection is performed by following these steps.

1. Create an interface which will be the contract for the dependency
2. Create a class that implements this interface
3. Register the service in the DI container. (In ASP.NET Core this will be in the Program.cs file)
4. Inject the service into the class that needs it via constructor, method, or property. (Constructor is the most common)


Implementing Dependency Injection

We will start with a model class taken from an ASP.NET Core MVC project. This model represents book categories.




The first thing we need to do is create an interface which is going to serve as the contract for our dependency. We will use the Repository Pattern for this interface. The Repository Pattern provides a layer of abstraction over data access, allowing for cleaner separation of concerns. When combined with Dependency Injection it enhances modularity and testability. 

 

Here ICategoryRepository implements IRepository and passes in a Category. It also has an Update method that will be used to update categories. Next we will create the class that implements this interface.

Here, the CategoryRepository class implements the ICategoryRepository interface and fulfills it's contract by providing a definition for the Update method.

Third we need to register the service in the DI Container which in ASP.NET Core is the Program.cs file.

The service lifetime is something that will not be covered in this article but in short there are 3 options for service lifetime, Transient, Scoped and Singleton. Here we are using scoped which lasts the duration of an HTTP request.

The last thing to do is to inject the service into the class that needs it. This will allow us to use the dependency in our code to update Categories.

We have "Injected" the ICategoryRepository into our CategoryController class so we are now able to use the dependency to update categories by calling _categoryRepo.Update(obj); and passing in a Category object.

Conclusion

In conslusion, Dependency Injection is a powerful pattern that enhances the modularity, testability, and maintainability of ASP.NET Core applications. By leveraging the built-in DI container, you can easily manage your application’s dependencies and lifetimes, leading to cleaner and more maintainable code.


I hope this article helps you understand Dependency Injection in ASP.NET Core! If you need further clarification check out the docs at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-8.0.

Programming ASP.NET Core Dependency Injection

Written by Brian McGaw

08/26/2024

Comments

No comments yet.