---
title: "Thought about DI in asp.net Core?"  
description: "Thought about DI in asp.net Core?"  
author: "Anubhav Sharma"  
published: 2026-03-29  
updated: 2026-03-29  
canonical: https://www.mindstick.com/interview/34484/thought-about-di-in-asp-dot-net-core  
category: "asp.net core"  
tags: [".net", "asp.net core"]  
reading_time: 4 minutes  

---

# Thought about DI in asp.net Core?

Dependency Injection (DI) in **ASP.NET Core** is a core design pattern used to **decouple components**, improve **testability**, and make your application **maintainable and scalable**.

## What is Dependency Injection?

**Dependency Injection** means:

> Instead of a class creating its own dependencies, those dependencies are **provided (injected)** from outside.

## Without DI (Bad Practice)

```cs
public class ArticleService
{
    private readonly SqlRepository _repo;

    public ArticleService()
    {
        _repo = new SqlRepository(); // tightly coupled
    }
}
```

### Problems:

- Hard to test
- Hard to replace implementation
- Tight coupling

## With DI (Good Practice)

```cs
public class ArticleService
{
    private readonly IRepository _repo;

    public ArticleService(IRepository repo)
    {
        _repo = repo; // injected
    }
}
```

## Built-in DI in ASP.NET Core

Unlike old ASP.NET MVC, **ASP.NET Core has DI built-in**.

You configure it in:

```plaintext
Program.cs
```

## Step 1: Register Services

```cs
builder.Services.AddScoped<IRepository, SqlRepository>();
builder.Services.AddScoped<IArticleService, ArticleService>();
```

## Step 2: Use in Controller

```cs
public class ArticleController : Controller
{
    private readonly IArticleService _service;

    public ArticleController(IArticleService service)
    {
        _service = service;
    }
}
```

ASP.NET Core automatically injects it.

## Service Lifetimes (Very Important)

### 1. Transient

```plaintext
AddTransient<IService, Service>();
```

New instance every time

### 2. Scoped (Most used)

```plaintext
AddScoped<IService, Service>();
```

One instance per request

### 3. Singleton

```plaintext
AddSingleton<IService, Service>();
```

One instance for entire app

## Which one should YOU use?

Based on your project:

| Use Case | Lifetime |
| --- | --- |
| DB Context / Repository | Scoped |
| Business Services | Scoped |
| Utility (stateless) | Singleton |
| Lightweight helper | Transient |

## Why DI is Important (Real Benefits)

### 1. Loose Coupling

You can switch:

```plaintext
SqlRepository → MongoRepository
```

without changing business logic

### 2. Testability

You can inject mock:

```plaintext
var mockRepo = new Mock<IRepository>();
```

### 3. Better Performance Control

With proper lifetime:

- Avoid memory leaks
- Avoid unnecessary object creation

### 4. Clean Architecture

Supports:

- Repository Pattern
- Service Layer
- Unit of Work

> DI in ASP.NET Core =\
> “Framework automatically provides required objects to your classes instead of you creating them manually.”

## Answers

### Answer by Anubhav Sharma

Dependency Injection (DI) in **ASP.NET Core** is a core design pattern used to **decouple components**, improve **testability**, and make your application **maintainable and scalable**.

## What is Dependency Injection?

**Dependency Injection** means:

> Instead of a class creating its own dependencies, those dependencies are **provided (injected)** from outside.

## Without DI (Bad Practice)

```cs
public class ArticleService
{
    private readonly SqlRepository _repo;

    public ArticleService()
    {
        _repo = new SqlRepository(); // tightly coupled
    }
}
```

### Problems:

- Hard to test
- Hard to replace implementation
- Tight coupling

## With DI (Good Practice)

```cs
public class ArticleService
{
    private readonly IRepository _repo;

    public ArticleService(IRepository repo)
    {
        _repo = repo; // injected
    }
}
```

## Built-in DI in ASP.NET Core

Unlike old ASP.NET MVC, **ASP.NET Core has DI built-in**.

You configure it in:

```plaintext
Program.cs
```

## Step 1: Register Services

```cs
builder.Services.AddScoped<IRepository, SqlRepository>();
builder.Services.AddScoped<IArticleService, ArticleService>();
```

## Step 2: Use in Controller

```cs
public class ArticleController : Controller
{
    private readonly IArticleService _service;

    public ArticleController(IArticleService service)
    {
        _service = service;
    }
}
```

ASP.NET Core automatically injects it.

## Service Lifetimes (Very Important)

### 1. Transient

```plaintext
AddTransient<IService, Service>();
```

New instance every time

### 2. Scoped (Most used)

```plaintext
AddScoped<IService, Service>();
```

One instance per request

### 3. Singleton

```plaintext
AddSingleton<IService, Service>();
```

One instance for entire app

## Which one should YOU use?

Based on your project:

| Use Case | Lifetime |
| --- | --- |
| DB Context / Repository | Scoped |
| Business Services | Scoped |
| Utility (stateless) | Singleton |
| Lightweight helper | Transient |

## Why DI is Important (Real Benefits)

### 1. Loose Coupling

You can switch:

```plaintext
SqlRepository → MongoRepository
```

without changing business logic

### 2. Testability

You can inject mock:

```plaintext
var mockRepo = new Mock<IRepository>();
```

### 3. Better Performance Control

With proper lifetime:

- Avoid memory leaks
- Avoid unnecessary object creation

### 4. Clean Architecture

Supports:

- Repository Pattern
- Service Layer
- Unit of Work

> DI in ASP.NET Core =\
> “Framework automatically provides required objects to your classes instead of you creating them manually.”


---

Original Source: https://www.mindstick.com/interview/34484/thought-about-di-in-asp-dot-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
