0
0
NestJSframework~5 mins

Dependency injection basics in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is Dependency Injection (DI) in NestJS?
Dependency Injection is a design pattern where NestJS automatically provides instances of classes (dependencies) to other classes that need them, instead of creating them manually.
Click to reveal answer
beginner
How does NestJS know which dependencies to inject?
NestJS uses decorators like @Injectable() to mark classes as providers and uses the constructor parameters to identify which dependencies to inject.
Click to reveal answer
beginner
What role does the @Injectable() decorator play in NestJS DI?
The @Injectable() decorator marks a class as a provider so NestJS can manage its lifecycle and inject it where needed.
Click to reveal answer
beginner
What is a provider in NestJS?
A provider is a class that can be injected as a dependency. It usually contains business logic or services that other parts of the app need.
Click to reveal answer
beginner
How do you inject a service into a controller in NestJS?
You add the service as a parameter in the controller's constructor. NestJS then injects the service instance automatically.
Click to reveal answer
Which decorator marks a class as injectable in NestJS?
A@Service()
B@Controller()
C@Module()
D@Injectable()
How does NestJS inject dependencies into a class?
ABy passing dependencies through the constructor
BBy calling a special inject() method
CBy manually creating instances inside the class
DBy using global variables
What is a provider in NestJS?
AA database connection
BA special type of controller
CA class that can be injected as a dependency
DA configuration file
Which of these is NOT a typical use of dependency injection?
ATo create tight coupling between classes
BTo make testing easier
CTo manage class lifecycles
DTo improve code modularity
Where do you declare providers so NestJS can inject them?
AIn the package.json file
BIn the module's providers array
CIn the main.ts file
DInside the controller's methods
Explain how dependency injection works in NestJS and why it is useful.
Think about how NestJS provides classes to others without manual creation.
You got /4 concepts.
    Describe the steps to inject a service into a controller in NestJS.
    Focus on decorators, module setup, and constructor usage.
    You got /4 concepts.