Dependency Injection in NestJS

Vishnu C Prasad
Stackademic
Published in
3 min readAug 10, 2023

Dependency injection is a powerful design pattern that promotes the separation of concerns and modularity in software applications. It allows you to achieve loosely coupled code, making your application more maintainable, testable, and scalable. In this article, we’ll dive into how dependency injection is implemented in NestJS, a popular TypeScript framework for building scalable and maintainable server-side applications.

Understanding Dependency Injection

At its core, dependency injection is the practice of supplying an external dependency to a class rather than creating it within the class itself. This helps to decouple components, making them more reusable and easier to manage.

In the context of NestJS, dependency injection involves letting the framework manage the creation and injection of dependencies into the components (controllers, services, and more) as needed. This is achieved through decorators, providers, and the NestJS IoC (Inversion of Control) container.

Providers in NestJS

In NestJS, a provider is a class that can be injected into other classes. It can be a service, repository, helper, or any other class that provides some functionality. Providers are decorated with the @Injectable() decorator.

Let’s look at an example of creating a simple service using the dependency injection pattern in NestJS:

// cars.service.ts
import { Injectable } from '@nestjs/common';

@Injectable()
export class CarsService {
private cars: string[] = [];

addCar(car: string) {
this.cars.push(car);
}

getCars(): string[] {
return this.cars;
}
}

In this example, the CarsService class is marked as injectable using the @Injectable() decorator. This service has methods to add and retrieve cars.

Injecting Dependencies

To inject a dependency into a class, you use the constructor of that class. The IoC container will automatically resolve and inject the required dependencies for you.

Let’s create a controller that uses the CarsService:

// cars.controller.ts
import { Controller, Get, Post, Body } from '@nestjs/common';
import { CarsService } from './cars.service';

@Controller('cars')
export class CarsController {
constructor(private readonly carsService: CarsService) {}

@Post()
addCat(@Body('name') name: string) {
this.carsService.addCar(name);
}

@Get()
getCars() {
return this.carsService.getCars();
}
}

In this example, the CarsController class injects the CarsService through its constructor. This enables the controller to use the methods provided by the service.

Module Configuration

In NestJS, you define the dependencies and their providers within modules. A module encapsulates a closely related set of components.

Let’s create a module that ties everything together:

// app.module.ts
import { Module } from '@nestjs/common';
import { CarsController } from './cars.controller';
import { CarsService } from './cars.service';

@Module({
controllers: [CarsController],
providers: [CarsService],
})
export class AppModule {}

In this module, we specify that the CarsController and CarsService are part of it. The IoC container will manage the creation and injection of dependencies.

Conclusion

Dependency injection is a fundamental concept in building maintainable and scalable applications. NestJS makes it easy to implement dependency injection using decorators, providers, and the IoC container. By separating concerns and injecting dependencies, you create code that is modular, testable, and easier to maintain.

In this article, we’ve explored the basics of dependency injection in NestJS, including creating injectable services, injecting dependencies into controllers, and configuring modules. This is just the tip of the iceberg, as NestJS provides more advanced features and techniques for managing dependencies in larger applications.

Thank you for reading until the end. Please consider following the writer and this publication. Visit Stackademic to find out more about how we are democratizing free programming education around the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Written by Vishnu C Prasad

As a dedicated software developer, I specialize in utilizing Flutter with Nest.js to create efficient and effective solutions.

No responses yet

Write a response