- Definition
- Types Of Dependencies in NestJS
- NestJS Dependency Injection
- Generate a Service using Nest CLI
-
Dependency: A class depends on another class.
-
Dependency Injection (DI) in NestJS is a technique used to implement Inversion of Control (IoC) for resolving dependencies.
-
Inversion of Control (IoC): The responsibility of creating and managing dependencies is delegated to the system (NestJS).
-
NestJS manages a dependency graph and knows the correct order in which to instantiate classes.
-
Instances of classes are treated as singletons, this means that only one instance of a class is created and shares across the application.
-
Intra-modular Dependency Injection: dependency within a single module, where one component or service depends on another within the same module.
-
Inter-Module Dependency Injection: dependencies between different modules.
-
Circular Dependencies: two modules depend on each other.
The @Injectable()
decorator makes the class available for dependency injection.
// users/providers/sign-in.provider.ts
import { Injectable } from "@nestjs/common";
export class SignInProvider {
public signIn() {
return true;
}
}
Each injectable class must be connected to the module it belongs.
// users/providers/users.service.ts
import { Injectable } from "@nestjs/common";
import { SignInProvider } from "./sign-in.provider";
@Injectable()
export class UsersService {
// Inject a provider to make its methods available for use
constructor(private readonly signInProvider: SignInProvider) {}
public signIn() {
return this.signInProvider.signIn();
}
}
The SignInProvider
provider is injected through the constructor.
// users/users.module.ts
import { Module } from "@nestjs/common";
import { UsersService } from "./providers/users.service";
import { SignInProvider } from "./providers/sign-in.provider";
@Module({
providers: [UsersService, SignInProvider],
exports: [UsersService],
})
export class UsersModule {}
This connection makes the providers available for other parts of the module to inject and use its functionality.
providers
: Connect the provider to share within this particular module.exports
: Make a provider available for sharing with other modules via DI.
// users/users.controller.ts
import { UsersService } from "./providers/users.service.ts";
@Controller()
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Post()
signIn() {
return this.usersService.signIn();
}
}
Constructor Injection: private readonly usersService: UsersService
in the constructor signals NestJS to inject an instance of UsersService
whenever UsersController
is instantiated.
nest generate service posts/providers/posts --flat