0
0
NestJSframework~5 mins

Provider scope (default, request, transient) in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the default provider scope in NestJS?
The default provider scope is Singleton. This means one instance of the provider is created and shared across the entire application.
Click to reveal answer
intermediate
Explain the Request provider scope in NestJS.
The Request scope creates a new instance of the provider for each incoming HTTP request. This helps isolate data per request.
Click to reveal answer
intermediate
What does the Transient provider scope do in NestJS?
The Transient scope creates a new instance of the provider every time it is injected, regardless of the request or module.
Click to reveal answer
advanced
How does the Request scope differ from the Transient scope?
The Request scope creates one instance per HTTP request, shared within that request. The Transient scope creates a new instance every time it is injected, even multiple times in the same request.
Click to reveal answer
intermediate
Why would you use Transient scope for a provider?
Use Transient when you want a fresh instance every time to avoid shared state or side effects, like for stateless helpers or utilities.
Click to reveal answer
What scope does NestJS use by default for providers?
ASession
BSingleton
CTransient
DRequest
Which provider scope creates a new instance for each HTTP request?
ASingleton
BTransient
CGlobal
DRequest
If you want a new provider instance every time it is injected, which scope should you use?
ASingleton
BRequest
CTransient
DScoped
Which scope shares the same instance within a single HTTP request but creates new instances for different requests?
ARequest
BSingleton
CTransient
DGlobal
Why might you avoid using Singleton scope for stateful providers?
ABecause Singleton shares the same instance across all requests, causing shared state issues
BBecause Singleton creates multiple instances
CBecause Singleton creates new instances per injection
DBecause Singleton is slower
Describe the differences between default, request, and transient provider scopes in NestJS.
Think about how many instances are created and when.
You got /3 concepts.
    When would you choose to use the transient scope over the request or default scopes in NestJS?
    Consider cases where sharing instances could cause problems.
    You got /3 concepts.