---
title: "What is dependency injection and how is it implemented in .NET?"  
description: "What is dependency injection and how is it implemented in .NET?"  
author: "ICSM Computer"  
published: 2025-06-16  
updated: 2025-06-16  
canonical: https://www.mindstick.com/interview/34246/what-is-dependency-injection-and-how-is-it-implemented-in-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# What is dependency injection and how is it implemented in .NET?

> **Dependency Injection (DI)** is a design pattern used to achieve **loose coupling** between classes by **injecting dependencies from the outside**, rather than a class creating its own dependencies.

## Why Use Dependency Injection?

- Improves **testability**
- Encourages **modular design**
- Enables **inversion of control (IoC)** — classes depend on abstractions, not implementations

## Real-World Analogy

> Instead of a class **baking its own cake**, it **asks for a cake to be delivered**.

## Without Dependency Injection (Tightly Coupled)

```cs
public class OrderService
{
    private EmailService _emailService = new EmailService(); // tightly coupled

    public void ProcessOrder()
    {
        // business logic
        _emailService.SendEmail();
    }
}
```

## With Dependency Injection (Loosely Coupled)

```cs
public interface IEmailService
{
    void SendEmail();
}

public class EmailService : IEmailService
{
    public void SendEmail() { /* send logic */ }
}

public class OrderService
{
    private readonly IEmailService _emailService;

    public OrderService(IEmailService emailService) // dependency injected
    {
        _emailService = emailService;
    }

    public void ProcessOrder()
    {
        _emailService.SendEmail();
    }
}
```

## How It's Implemented in .NET (ASP.NET Core)

### 1. Register services in `Startup.cs` (or `Program.cs` in .NET 6+)

```plaintext
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<OrderService>();
```

### 2. Constructor injection (automatically handled by framework)

```cs
public class HomeController : Controller
{
    private readonly OrderService _orderService;

    public HomeController(OrderService orderService)
    {
        _orderService = orderService;
    }

    public IActionResult Index()
    {
        _orderService.ProcessOrder();
        return View();
    }
}
```

## Types of DI in .NET

| Type | Description |
| --- | --- |
| **Constructor Injection** | Most common, safe for required dependencies |
| **Property Injection** | Optional dependencies |
| **Method Injection** | Dependencies passed to methods |

## Lifetime Scopes

| Method | Lifetime |
| --- | --- |
| `AddSingleton<T>()` | One instance for the whole app |
| `AddScoped<T>()` | One per request |
| `AddTransient<T>()` | New instance every time it's requested |

## Benefits

- Makes unit testing easier (e.g., mock services)
- Reduces tight coupling
- Promotes separation of concerns
- Enables inversion of control

## Answers

### Answer by ICSM Computer

> **Dependency Injection (DI)** is a design pattern used to achieve **loose coupling** between classes by **injecting dependencies from the outside**, rather than a class creating its own dependencies.

## Why Use Dependency Injection?

- Improves **testability**
- Encourages **modular design**
- Enables **inversion of control (IoC)** — classes depend on abstractions, not implementations

## Real-World Analogy

> Instead of a class **baking its own cake**, it **asks for a cake to be delivered**.

## Without Dependency Injection (Tightly Coupled)

```cs
public class OrderService
{
    private EmailService _emailService = new EmailService(); // tightly coupled

    public void ProcessOrder()
    {
        // business logic
        _emailService.SendEmail();
    }
}
```

## With Dependency Injection (Loosely Coupled)

```cs
public interface IEmailService
{
    void SendEmail();
}

public class EmailService : IEmailService
{
    public void SendEmail() { /* send logic */ }
}

public class OrderService
{
    private readonly IEmailService _emailService;

    public OrderService(IEmailService emailService) // dependency injected
    {
        _emailService = emailService;
    }

    public void ProcessOrder()
    {
        _emailService.SendEmail();
    }
}
```

## How It's Implemented in .NET (ASP.NET Core)

### 1. Register services in `Startup.cs` (or `Program.cs` in .NET 6+)

```plaintext
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<OrderService>();
```

### 2. Constructor injection (automatically handled by framework)

```cs
public class HomeController : Controller
{
    private readonly OrderService _orderService;

    public HomeController(OrderService orderService)
    {
        _orderService = orderService;
    }

    public IActionResult Index()
    {
        _orderService.ProcessOrder();
        return View();
    }
}
```

## Types of DI in .NET

| Type | Description |
| --- | --- |
| **Constructor Injection** | Most common, safe for required dependencies |
| **Property Injection** | Optional dependencies |
| **Method Injection** | Dependencies passed to methods |

## Lifetime Scopes

| Method | Lifetime |
| --- | --- |
| `AddSingleton<T>()` | One instance for the whole app |
| `AddScoped<T>()` | One per request |
| `AddTransient<T>()` | New instance every time it's requested |

## Benefits

- Makes unit testing easier (e.g., mock services)
- Reduces tight coupling
- Promotes separation of concerns
- Enables inversion of control


---

Original Source: https://www.mindstick.com/interview/34246/what-is-dependency-injection-and-how-is-it-implemented-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
