All quizzesHard
DI Container Internals — Series 3
Preview — 3 of 10 questions
None of these appear in any providers array. How are they resolvable?
javascript
constructor(
private readonly moduleRef: ModuleRef,
private readonly reflector: Reflector,
private readonly adapterHost: HttpAdapterHost,
) {}AThey are resolved by name from the @nestjs/core package at injection time
BThe container registers a small set of internal providers into every module's context while building the graph — ModuleRef, Reflector, ApplicationConfig, HttpAdapterHost and the like — so they resolve through the ordinary injector with no application registration
CThey are global modules the CLI adds to AppModule during the build
DThey are injected by a special-case branch in the injector that bypasses token resolution
What makes this efficient in an application with hundreds of modules?
javascript
this.moduleRef.get(SomeService, { strict: false });AIt walks the module graph breadth-first on each call, stopping at the first match
BIt resolves against a copy of the graph serialised at startup
CIt falls back to require()-ing the token's source file
DAfter scanning, the container builds an index from token to the wrappers providing it across all modules, so a non-strict lookup is a map read rather than a traversal — with the ambiguity that a token registered in several modules has several entries, and the index decides which one you get
How does the container avoid constructing it twice?
javascript
Two branches of the graph both depend on DatabaseService,
whose factory is asynchronous and takes 300 ms.AThe wrapper records that instantiation is in progress and stores the pending promise, so a second resolution awaits that same promise instead of starting a parallel construction — which is what makes an expensive async factory run exactly once
BThe two branches are resolved sequentially, so concurrency never arises
CBoth constructions proceed and the later result overwrites the earlier one
DA mutex is acquired on the module, blocking all other resolutions until the factory completes
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.