Back to course

Dependency Injection Basics

C# Zero to Hero: Comprehensive Programming Masterclass

Dependency Injection (DI)

DI is a design pattern used to achieve Inversion of Control (IoC). It makes your code easier to test and maintain.

How it works

Instead of creating an object inside a class, you "inject" it through the constructor.

csharp public class UserService { private readonly IUserRepository _repo; public UserService(IUserRepository repo) // Injected { _repo = repo; } }