← Foundations path
.NETFoundations 22 / 37

Resolving a service

“How does a controller get its LoanService? What does it mean to resolve a service?”

Who asks the container for an object, when they ask, and what goes wrong when a class asks for itself.

See it

GET /api/loans/{id}containerLoansControllerILoanService → LoanServiceILoanRepositoryICountryRulesTimeProvidersingletons: built once, then reused200 OK · EMI 10,501
1 / 13A request arrives: GET /api/loans/{id}. An object must handle it: a .

In plain words

Resolving means asking the container for a ready-made object. When a request comes in, the framework needs a . It asks the container for what the controller needs. The container sees a loan service is needed, which needs three more things. It builds them in order and hands everything back. Your classes only list what they need in their constructor. They never build it, and never go looking for it.

Say it in the interview

In the lending API

Four files from the lending API: the normal way, the bug, and a test. Press Next line to walk through them.

Lending.sln · where these 4 files sit
  • src/
    • Api/Controllers/Servicing/
    • Application/Servicing/Loans/
  • tests/Api.IntegrationTests/
Diagram · step 1A request arrives: GET /api/loans/{id}. An object must handle it: a .
GET /api/loans/{id}containerLoansControllerILoanService → LoanServiceILoanRepositoryICountryRulesTimeProvidersingletons: built once, then reused200 OK · EMI 10,501
LoansController.cs · line 6 · 1 of 5Requests to /api/loans/... come to this controller. The framework creates a new one for every request.

What they ask next

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

Traps

With AI tools

Canva grades whether a candidate finds and fixes problems in AI-generated code. One to look for here: generated code that takes and calls inside a method, for example to get round a lifetime error. It compiles, and passes every test until a registration is missing. Ask of every class: does its constructor list everything it needs?

Read more: Microsoft Learn: dependency injection in ASP.NET Core · Microsoft Learn: dependency injection into controllers · Microsoft Learn: DI guidelines (service locator) · ServiceProviderOptions.ValidateOnBuild