Dependency Injection in NestJS
Last Updated :
19 Jul, 2024
Dependency Injection (DI) is a fundamental concept in modern software development, enabling developers to create modular, maintainable, and testable code. NestJS, a progressive Node.js framework, uses DI to manage the dependencies of various components in an application. In this article, we'll explore how dependency injection works in NestJS.
What is Dependency Injection?
Dependency Injection is a design pattern that allows an object to receive other objects it depends on, rather than creating them internally. This promotes loose coupling, making code more modular and easier to manage.
How Dependency Injection Works in NestJS
Dependency Injection (DI) in NestJS operates through its Inversion of Control (IoC) container, which automatically manages the representation and injection of dependencies. When a class is marked with the @Injectable() decorator, it becomes a provider that can be injected into other classes. These dependencies are specified in the constructor of a class, and the IoC container resolves them at runtime. This allows for clear and maintainable code, as dependencies are not created manually but injected as needed.
Key Concepts in NestJS DI
- Providers: Classes that can be injected as dependencies. They are usually services or repositories.
- Controllers: Handle incoming requests and return responses.
- Modules: Group related providers and controllers together.
Steps to Implement Dependency Injection
Step 1: Setting Up a NestJS Project
First, create a new NestJS project using the Nest CLI.
npm i -g @nestjs/cli
nest new nest-gfg
cd nest-gfg
Step 2: Creating a Service
Create a service that will be injected into a controller. Services are the most common types of providers.
nest generate service hello
Step 3: Creating a Controller
Create a controller that will use the HelloService.
nest generate controller hello
Folder Structure
Folder StructureDependencies
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mongoose": "^10.0.10",
"@nestjs/platform-express": "^10.0.0",
"dotenv": "^16.4.5",
"mongoose": "^8.5.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
}
Example: Implementing basic Dependency Injection
JavaScript
//hello.controller.ts
import { Controller, Get } from '@nestjs/common';
import { HelloService } from './hello.service';
@Controller('hello')
export class HelloController {
constructor(private readonly helloService: HelloService) { }
@Get()
getHello(): string {
return this.helloService.getHello();
}
}
JavaScript
//hello.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class HelloService {
getHello(): string {
return 'Hello, World!';
}
}
JavaScript
//app.module.ts
import { Module } from '@nestjs/common';
import { HelloService } from './hello/hello.service';
import { HelloController } from './hello/hello.controller';
@Module({
imports: [],
controllers: [HelloController],
providers: [HelloService],
})
export class AppModule { }
Run the application using the following command:
npm run serve
Output
Dependency Injection in NestJS