← Foundations path
.NETFoundations 20 / 37Reported by candidates

The DI container

“What is dependency injection, and what does the DI container do?”

The part of .NET that builds your objects, and hands them to the classes that need them.

See it

Before: the controller calls newAfter: the container calls newDI containerLoansController_loans = new LoanService( new InMemoryLoanRepository(), new IndiaRules(), TimeProvider.System);POST → 201GET → 404each request: new controller, new empty repoLoansController(ILoanService)LoanServiceits constructor asks for:ILoanRepository loansICountryRules rulesTimeProvider clockLoanServicebuilt with:InMemoryLoanRepositoryIndiaRulesTimeProvider.SystemInMemoryLoanRepositoryfor ILoanRepositoryIndiaRulesfor ICountryRulesTimeProvider.Systemneeds ILoanServiceILoanRepository: ?edit here, and in every class like itPOST → 201, GET → 200Unable to resolve ILoanRepository
1 / 12Before: builds its own parts with , and the parts of its parts too.

In plain words

Your classes stop building their own parts. Each one only lists what it needs, in its constructor. When the app runs, the DI container, which is part of .NET, reads that list, builds every part first, then builds your class and hands the parts in. That is dependency injection. To swap a part, you change one line in Program.cs, not every class that uses it.

Say it in the interview

In the lending API

The controller before and after, the service whose constructor the container reads, the registrations, and a test with no container at all. Press Next line to walk through them.

Lending.sln · where these 5 files sit
  • src/
    • Api/
      • Controllers/Servicing/
    • Application/Servicing/Loans/
  • tests/Application.UnitTests/Servicing/Loans/
Diagram · step 3A database repo, or a fake in a test? You must edit this file, and every class that wrote .
Before: the controller calls newAfter: the container calls newDI containerLoansController_loans = new LoanService( new InMemoryLoanRepository(), new IndiaRules(), TimeProvider.System);POST → 201GET → 404each request: new controller, new empty repoLoansController(ILoanService)LoanServiceits constructor asks for:ILoanRepository loansICountryRules rulesTimeProvider clockLoanServicebuilt with:InMemoryLoanRepositoryIndiaRulesTimeProvider.SystemInMemoryLoanRepositoryfor ILoanRepositoryIndiaRulesfor ICountryRulesTimeProvider.Systemneeds ILoanServiceILoanRepository: ?edit here, and in every class like itPOST → 201, GET → 200Unable to resolve ILoanRepository
LoansController.old.cs · line 4 · 1 of 6The API layer now imports the rules and the repository's own folders. It is tied to every concrete class.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

With AI tools

AI tools now write the constructors and the Program.cs lines for you. A question reported in the AI-era research: "AI generated this snippet. How do you check it is correct?" For DI code the check is walking the object graph: for each constructor parameter, which registration answers it, which class gets built, and when. A suggested registration change that only makes an error go away fails that check.

Read more: Microsoft Learn: DI in ASP.NET Core · Microsoft Learn: .NET dependency injection · Microsoft Learn: DI guidelines · Martin Fowler: IoC containers and DI