MΓ³dulo 05 Β· NestJS: Modules, Controllers, Services β LecciΓ³n 3 de 3 Β· ~9 min
Dependency injection: the big idea
Where did this.walkers come from?
The controller used this.walkers.findOne(id) β but it never wrote new WalkersService(). That's dependency injection (DI), and it's the single idea that makes Nest worth using.
You declare what you need as a constructor parameter, and Nest's container provides it. The controller says I need a WalkersService; Nest builds one (once) and passes it in. The class never reaches out for its dependencies β they're handed to it. That inversion is the whole trick.
constructor(private readonly walkers: WalkersService) {}