Folks, my newest blog is all about the ASP.NET Core dependency injection! First off, I will explain the concept and the theory behind it. Then let’s examine how it is being applied in .NET Core.
I’m going to discuss the following pointers in detail:
- What is Dependency Injection?
- Register a Service in ASP.NET Core DI in Container.
- Methods in ASP.NET Core to register a service with Dependency Injection.
- Understanding the Singleton, Scoped, and Transient Methods
- Advantages of using ASP.NET Core Dependency Injection
So, let’s jump in!
What is Dependency Injection (DI)?
This comic explains it…
Fig 1: What is Dependency Injection (DI)?
Source: https://www.freecodecamp.org/
The concept of dependency injection, in software engineering, is used to supply the dependencies of another object with a single object (or static method). Think of a dependency as an object that you can use (a service).
Scale and extend your business solutions with agile repository patterns and dependency injection.
Let’s walk through the step-by-step procedure to execute dependency injection in the ASP.NET Core MVC application.
Dependency Injection in ASP.NET Core:
The ASP.NET Core Framework is designed from scratch for Dependency Injection. With the inversion of control (IoC) container built into ASP.NET Core Framework, dependency classes are injected through constructors or methods.
Fig 2: Dependency Injection in ASP.NET Core
When the object of class B is passed in class A, the object is used by methods of Class A, to perform its function. Thus, we understand that Class A has dependency on the Class B object.
Injecting Dependency means that you push the dependency (object of class B) into class A from outside.
Dependency injection can be divided into three types:
- Constructor injection: The dependencies are offered via a class constructor.
- Method injection: It is used when the instance of a dependency is only required within a single method in the entire component.
- Property: It is used when a property of the component holds the dependency instance, and it is resolved during runtime by the container. It is not possible to inject this type of code in ASP.NET Core.
Let’s focus on the statement “object pushed from outside.”
- Therefore, we should not initiate dependencies/objects using the new operator.
Example:
- Alternatively, we can pass dependency to the constructor or setter. There are plenty of other ways to inject dependency, as well.
Example: Injection via constructor
How can you register a service in ASP.NET Core DI in Container? Let’s find out.
Register a Service in ASP.NET Core DI in Container
You must register a service with ASP.NET Core Dependency Injection Container in the Services () method of the program class.
Determining how many times the dependency object should be created depends on its lifetime.
Now let’s learn about the methods in ASP.NET Core to register a service with dependency injection.
Methods in ASP.NET Core to register a service with Dependency Injection
The instance and lifespan of the dependency determine the service’s lifetime.
1. Singleton
- Each request will create only one instance, which will be used throughout the application.
- Throughout the application, the same instance was shared.
2. Scoped
- Each scope will be created as a single instance.
- It will create instances of every request.
3. Transient
- It will create a new instance every time and not share it with other applications.
- It is used for small, lightweight applications.
Now let’s create a .NET Core Application using Dependency Injection, one by one.
Then configure the project, here the given name is ServiceLifecycleDemo.
Add new interfaces- IScopedService, ISingletonService, ITransientService
Add the following method in all the three interfaces like the IScopedService.CS file.
Add new OperationService.cs class files and put the following code:
Now, Register the services inside the IServiceCollection in program class.
Now, add the following HTML code in HomeController > Index action method > index view.
<div class="text-center"> <h2 class="display-4"> Dependency Injection Lifetime </h2> </div> <table class="table table-bordered"> <thead> <tr> <th>Service Type</th> <th>First Instance Operation ID</th> <th>Second Instance Operation ID</th> </tr> </thead> <tbody> <tr> <td style="background-color: darksalmon"> Transient </td> <td style="background-color: darksalmon"> @ViewBag.transient1 </td> <td style="background-color: darksalmon"> @ViewBag.transient2 </td> </tr> <tr> <td>Scoped</td> <td>@ViewBag.scoped1</td> <td>@ViewBag.scoped2</td> </tr> <tr> <td style="background-color: aquamarine"> Singleton </td> <td style="background-color: aquamarine"> @ViewBag.singleton1 </td> <td style="background-color: aquamarine"> @ViewBag.singleton2 </td> </tr> </tbody> </table>
Constructor Injection in ASP.NET Core MVC Application
Whenever a service type is included in a constructor, the IoC container automatically performs a constructor injection.
Modify the HomeController as shown below. You can see that we inject ITransientService, IScopedService, ISingletonService service into the constructor easily without being tightly coupled.
Now run the application.
For the two types of services, we will see two different Guids. Now run two instances of UI in two different tabs of the browser like request 1 and request 2.
Request 1
Request 2
Observation from Request 1 and Request 2
1. Transient service: In a transient service, even though the request is the same, it returns a new instance, so the operation ID for Request 1 and Request 2 are different.
2. Scoped service: In a scoped service, a single instance is created for each request, which is shared across the requests.
However, if we click the Refresh button or open the UI in a different tab in a browser, new IDs are generated.
3. Singleton service: One instance of a singleton service is created and shared across the applications.
Now it’s time to look at why it’s beneficial to use a ASP.NET Core dependency injection.
Advantages of using ASP.NET Core Dependency Injection
Here are the three top advantages:
1. It helps you to develop loosely coupled applications.
2. It helps you to develop highly maintainable code.
3. It helps you to develop testable code.
Well, this blog is devoted to all things ‘dependency injection’. I trust you are now familiar with it.
Write to us with your thoughts about it and visit us at Nitor Infotech to learn more about what we do.
Happy coding!