What are custom providers and provider scopes?
Try code in the playgroundUnderstand the concept
A provider is a dependency that Nest can supply, such as a class instance, value or factory result. Custom providers control how that dependency is created, and scope controls how long its instances are shared. These choices affect testability, state isolation and allocation cost.
Important ideas
-
useValue: supply a value;useClass: supply a class;useFactory: build a dependency. -
Singleton is the default; request scope creates an instance per request.
-
Transient scope gives each consumer its own instance; extra instances add overhead.
-
Do not store one user's request data on a singleton service field; concurrent requests can observe shared state.
Can you explain it without the notes?
What are custom providers and provider scopes?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
A provider is a dependency that Nest can supply, such as a class instance, value or factory result. Custom providers control how that dependency is created, and scope controls how long its instances are shared. These choices affect testability, state isolation and allocation cost.
Turn this concept into a clear answer.
- Choose request scope only when lifecycle requirements justify it; singleton is not a license to keep request-specific mutable state.
Adapted from 03_NESTJS.md · “What are custom providers and provider scopes?”. Examples are study fragments; framework behavior can depend on your version.