Lesson 6 / 33

Dependency Injection

NestJS's powerful IoC container manages dependencies for you.

Inject via constructor

NestJS automatically instantiates and injects services via the constructor. No manual new needed.

@Injectable()
export class UsersService {
  // service logic
}

@Controller('users')
export class UsersController {
  constructor(private usersService: UsersService) {}

  @Get()
  findAll() {
    return this.usersService.findAll();
  }
}

Lazy loading & scopes

By default, services are singletons (one instance for the app). You can also use REQUEST or TRANSIENT scopes for per-request or per-use instances.