×
Ganesh Kadam
Lead Engineer
Ganesh Kadam is a lead engineer with extensive experience in as a full-stack .NET environment. He has a passion for designing and implementing ... Read More

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:

  1. What is Dependency Injection?
  2. Register a Service in ASP.NET Core DI in Container.
  3. Methods in ASP.NET Core to register a service with Dependency Injection.
  4. Understanding the Singleton, Scoped, and Transient Methods
  5. Advantages of using ASP.NET Core Dependency Injection

So, let’s jump in!

What is Dependency Injection (DI)?

This comic explains it…

What is Dependency Injection (DI) Nitor Infotech

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.

Dependency Injection in ASP.NET Core 1 Nitor Infotech

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:

  1. Constructor injection: The dependencies are offered via a class constructor.
  2. Method injection: It is used when the instance of a dependency is only required within a single method in the entire component.
  3. 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:

Dependency Injection in ASP.NET Core 2 Nitor Infotech

  • 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
Dependency Injection in ASP.NET Core 3 Nitor Infotech

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.

Register a Service in ASP.NET Core DI in Container Nitor Infotech

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.

Creating a .NET Core Application 1 Nitor Infotech

Then configure the project, here the given name is ServiceLifecycleDemo.

Creating a .NET Core Application 2 Nitor Infotech

Creating a .NET Core Application 3 Nitor Infotech

Add new interfaces- IScopedService, ISingletonService, ITransientService

Creating a .NET Core Application 4 Nitor Infotech

Add the following method in all the three interfaces like the IScopedService.CS file.

Creating a .NET Core Application 5 Nitor Infotech

Add new OperationService.cs class files and put the following code:

Creating a .NET Core Application 6 Nitor Infotech

Now, Register the services inside the IServiceCollection in program class.

Creating a .NET Core Application 7 Nitor Infotech

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.

Constructor Injection in ASP.NET Core MVC Application 1 Nitor Infotech

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

Constructor Injection in ASP.NET Core MVC Application 2 Nitor Infotech

Request 2

Constructor Injection in ASP.NET Core MVC Application 3 Nitor Infotech

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!

subscribe image

Subscribe to our
fortnightly newsletter!

we'll keep you in the loop with everything that's trending in the tech world.

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. Accept Cookie policy