Dependency Injection in .NET Core – A Detailed Guide
Tags: dotnet, dotnetcore, programming, softwaredevelopment
Published: 2023-10-13 15:30:30

Dependency injection (DI) is a technique widely used in .NET Core to implement the inversion of control (IoC) principle for resolving dependencies between classes. It helps create loosely coupled code that is easier to test, maintain, and extend over time.

## What is Dependency Injection?
At its core, dependency injection aims to separate the creation and use of object dependencies. Rather than having classes create their own dependencies directly using new, dependencies are provided to classes from an external source. This external provider is known as an injector and is responsible for instantiating dependencies and supplying them to client classes as needed.

The client classes expose their dependencies via constructor parameters, method parameters, or public properties. They do not worry about how the dependencies are created or managed - the injector handles that. This separation of concerns enables loose coupling between classes and their dependencies.

## Why Use Dependency Injection?
There are several key benefits to using DI:

- **Removes tight coupling between classes:** Classes have no knowledge of how their dependencies are created, only that they will be provided from somewhere. This makes code more modular and flexible.
- **Enables easy mocking of dependencies for testing:** Dependencies can be swapped for mock objects that simulate behavior without complex real implementations.
- **Simplifies switching implementations:** The injector configures which dependency implementations to use. Switching implementations does not require any changes to client classes.
- **Promotes code reuse:** Common dependencies can be shared across multiple classes through the injector.
- **Configuration through code:** Dependency configuration is specified in code rather than through manual wiring. This makes it easier to track dependencies and how they are created.

## Using the Built-In .NET Core DI Container
.NET Core includes a simple built-in dependency injection container that can be used to manage class dependencies. Here is a quick overview of how to use it:

**1. Install required NuGet packages**
The Microsoft.Extensions.DependencyInjection package provides the core DI functionality. This needs to be referenced from any project using the built-in container.

**2. Create a service interface and implementation**
Define interfaces for any services you want to abstract via DI. Then, create concrete implementation classes of those interfaces.

```
public interface IMessageService 
{
    string GetMessage();
}

public class MessageService : IMessageService
{
    public string GetMessage()
    {
        return "Hello World!";
    }
}
```
**3. Configure services in Startup.cs
**In the ConfigureServices method, add each service interface and implementation to the service collection:

```
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IMessageService, MessageService>();
}
```
This registers MessageService as a singleton implementation of IMessageService. Other lifetimes, like scoped and transient, are also available.

4. Inject service through constructor
Wherever the service is needed, inject it via the constructor and use it:

```
public class MyClass 
{
    private IMessageService _messageService;

    public MyClass(IMessageService messageService)
    {
        _messageService = messageService;
    }

    public string GetMessage()
    {
        return _messageService.GetMessage();
    }
}
```
The injector will provide an instance of MessageService automatically when creating MyClass.

This covers the basics of using DI through the built-in .NET Core container. There are many more advanced capabilities as well.

## Why Use a Separate Container?

While usable, the built-in .NET Core DI container is fairly basic. For more complex scenarios, a dedicated container like Autofac or StructureMap is recommended. These provide capabilities like:

- Interception to enable cross-cutting concerns like logging.
- Fine-grained control over lifetime and scoping rules.
- Explicit relationship management between services.
- Advanced registration options and middleware.

Most containers integrate seamlessly with .NET Core through an adapter package. So, switching from the built-in container is quite simple.

DI Frameworks like Scrutor can also help streamline the registration of many services from an assembly. Overall, standalone DI containers are a great way to harness the full power of dependency injection in .NET Core apps.

[Dependency injection in .NET Core](https://positiwise.com/blog/dependency-injection-in-net-core-with-example) is an invaluable technique for building loosely coupled and well-structured .NET Core applications. Using the built-in DI container provides a great introduction to the concept. Switching to a more robust container opens up many more architectural possibilities as an application grows.