Question presented to candidate:
"In a providers array I see useClass, useValue, useFactory and useExisting. Explain what each one does and give a real situation where you would pick it."
What a strong answer should cover:
- A provider is
{ provide: TOKEN, <recipe> }; the recipe says how the value for that token is produced, and the result is cached per injector. useClass: instantiate this class (possibly a different one than the token), e.g. a mock or an environment-specific implementation. The bare[MyService]shorthand means{ provide: MyService, useClass: MyService }.useValue: hand over this exact value, no construction: config objects, constants, test fakes.useFactory: run a function to decide or build the value; it can callinject()(or use the olderdepsarray) and runs once per injector.useExisting: an alias: the token resolves to whatever another token resolves to, the same instance, not a copy. Contrast withuseClass, which builds a new instance under the second token.multi: truecombines with any recipe to build a list.
Clarifying questions expected:
- "Is the alternative implementation chosen at build time, at startup, or per subtree?"
Code / implementation expected: Yes β all four recipes with instance identity checked.