0
0
NestJSframework~5 mins

Custom providers in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom provider in NestJS?
A custom provider in NestJS is a way to define how a dependency is created or supplied, allowing you to control the instance or value injected into your classes.
Click to reveal answer
beginner
How do you define a custom provider using the 'useValue' syntax?
You define a custom provider by specifying an object with a 'provide' key for the token and a 'useValue' key for the fixed value to inject, like { provide: 'TOKEN', useValue: someValue }.
Click to reveal answer
intermediate
Explain the difference between 'useClass' and 'useFactory' in custom providers.
'useClass' tells NestJS to instantiate a specific class when injecting, while 'useFactory' lets you provide a function that returns the value or instance, allowing dynamic or async creation.
Click to reveal answer
intermediate
What is the purpose of the 'inject' array in a custom provider using 'useFactory'?
The 'inject' array lists the dependencies that NestJS should pass as arguments to the factory function, enabling you to use other providers inside your factory.
Click to reveal answer
intermediate
Why would you use a custom provider instead of a regular service class in NestJS?
Custom providers let you control exactly how and what is injected, useful for injecting constants, third-party libraries, or creating dynamic or mocked instances for testing.
Click to reveal answer
Which property do you use to provide a fixed value in a custom provider?
AuseFactory
BuseClass
CuseValue
DuseExisting
What does the 'useClass' property do in a custom provider?
ASpecifies a class to instantiate
BReuses an existing provider
CDefines a factory function
DReturns a fixed value
How do you inject dependencies into a factory function in a custom provider?
AUsing 'useClass'
BUsing the 'inject' array
CUsing 'useValue'
DUsing 'provide'
Which custom provider type allows asynchronous creation of the injected value?
AuseFactory
BuseClass
CuseValue
DuseExisting
What is a common use case for custom providers in NestJS?
ATo create UI components
BTo style HTML elements
CTo write SQL queries
DTo control dependency injection behavior
Describe how to create a custom provider in NestJS using 'useFactory' and explain when you might use it.
Think about when you need to create a value dynamically or asynchronously.
You got /4 concepts.
    Explain the differences between 'useValue', 'useClass', and 'useFactory' in NestJS custom providers.
    Consider how each method controls what gets injected.
    You got /4 concepts.