Written by Mark Pringle | Last Updated on Monday, November 28, 2022

ASP.NET Core ASP.NET Version: 6.0 General Information

Dependency injection (DI) is a software design pattern that enables the development of code in which one object or class in the application uses another object or class in the application. In this pattern, you are “injecting” a class or object into another class or object to allow them to collaborate or work with each other. One class becomes dependent on another at runtime instead of being manually created or hardcoded as a single class. This is called loose coupling.

For example, suppose Class D needs Class E to do its job. DI allows Class D to use Class E. Then, class D depends on Class E. Imagine if you had to hardcode a new DE Class instead of combining existing classes. This pattern enables us to manage future code changes and application complexity more effectively.

ASP.NET Dependency

The DI pattern involves 3 types of classes.

  1. Client Class: This is the class that depends on the service class. In the above scenario, Class D is the client class.
  2. Service Class: This is the class that provides service to the client class. In the above scenario, Class E is the service class.
  3. Injector Class: This is the class that injects the service class object into the client class.

What is a DI Container?

Imagine if you had to create a new object or class for each usage when other objects exist that have the methods or properties you need. That would make a bulky or monolithic model set. Wouldn’t it be great to use things from other objects and inject them into a single entity? A DI Container facilitates this. It creates an object from all of the required dependencies by using the dependencies’ methods or properties. These dependencies are objects now dependent on another object but inherently separate. This allows applications to be more testable, modular, and maintainable.

You may have heard a programmer use Inversion of Control (IoC) instead of Dependency Injection (DI). These two phrases are used to describe the same design pattern, or more specifically, the container used to inject dependencies.

There are many third-party DI or IoC containers available for ASP.NET Core. However, by default, ASP.NET Core injects objects of dependency classes through a constructor or method by using its built-in IoC container. The built-in container is represented by the IServiceProvider. This interface simplifies Dependency Injection management.

DI Container Lifecycle

This DI Container can live or have a Lifecycle as long as you desire. There are stages to its lifecycle, and the container can have various scopes.

  1. Register – Initiate the DI and define its scope: transient, scoped, and singleton.
    • Transient - A new class instance is created every time it is asked for by the DI container.
    • Scoped - The class is created once per web request and is shared in a single request.
    • Singleton - A single class instance will be created for the entire application lifetime but is only instantiated (created) once in a given scope. However, multiple scopes can be created.
  2. Resolve – The DI creates an object from the required dependencies at this stage. Aren’t you glad you didn’t have to create this object manually?
  3. Dispose – Once the container is no longer needed, it must be disposed of.

See also: Types of Dependency Injection in ASP.NET Core.