NashTech Blog

Table of Contents
woman in blue suit jacket

Dependency Injection (DI) is a powerful pattern widely used in today’s software development in industry, including .NET Core applications. In this blog now we will explore what Dependency Injection is, why it’s important, and how it’s implemented in .NET Core.

What is Dependency Injection?

At its core, Dependency Injection is a design pattern that allows components in an application to depend on abstractions rather than concrete implementations. This promotes loose coupling between components, making our code base more modular, testable, and maintainable.

In simple terms, Dependency Injection helps manage the dependencies of our classes by providing them with the objects they need (dependencies) from external sources rather than creating those objects themselves inside classes.

Why we should use Dependency Injection?

There are several benefits to using Dependency Injection:

  1. Modularity: DI encourages modular design by breaking down an application into smaller, more manageable components.
  2. Testability: By decoupling components, DI makes it easier to write unit tests for individual components without needing to instantiate complex dependency graphs. It helps us in mocking our classes.
  3. Maintainability: DI reduces the coupling between components, making it easier to maintain and evolve the code base over time.
  4. Flexibility: With DI, we can easily swap out implementations of dependencies without modifying the dependent classes.
Dependency Injection in .NET Core

.NET Core has built-in support for Dependency Injection through its built-in container, which is part of the Microsoft.Extensions.DependencyInjection namespace. Here’s how we can use DI in .NET Core:

  1. Register Services: Begin by registering your services (dependencies) with the DI container in the ConfigureServices method of our Startup class. We can do this using methods like AddTransient, AddScoped, or AddSingleton depending on the desired lifetime of our service.
  2. Inject Dependencies: Once our services are registered, we inject them into our classes using constructor injection. We can simply declare a constructor parameter of the type we want to inject, and the DI container will provide an instance of that type when creating an instance of our class.
  3. Use Services: With our dependencies injected, we can now use them within our classes as needed. Since the DI container manages the lifetime and instantiation of our services, we don’t need to worry about creating or disposing of them manually.

Example

How to register services ?

We can register services using following three ways.

  • AddTransient : Registers a service with a new instance created every time when requested.
  • AddScoped : Registers a service with a new instance created once per request within the scope of the request.
  • AddSingleton : Registers a service with a single instance shared across the entire application lifetime.

services.AddTransient<IMyService, MyService>();

services.AddSingleton<IMyService, MyService>();

services.AddScoped<IMyService, MyService>();

Implementation of IMyService registered as AddTransient

                services.AddTransient<IMyService, MyService>();

public class MyService
{
private readonly IMyService _dependency;

public MyService(IMyService dependency)
{
_dependency = dependency;
}

public void DoSomething()
{
_dependency.DoSomethingElse();
}
}

In this example, MyService depends on IMyDependency, and we inject via its constructor. The actual implementation of IMyDependency is provided by the DI container based on the configuration.

Conclusion

Dependency Injection is a fundamental concept in modern software development, and .NET Core provides robust support for it out of the box. By leveraging DI, we can build more modular, testable, and maintainable applications. So, next time we’re designing a .NET Core application, we need to remember to embrace Dependency Injection for cleaner and more maintainable code.

Picture of Ajay Jajoo

Ajay Jajoo

Leave a Comment

Suggested Article

Discover more from NashTech Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading