-
Notifications
You must be signed in to change notification settings - Fork 536
Dependency injection container
Dependency injection is a way to implement the Inversion of control pattern. The core idea of this pattern is that every dependency of a object that can (and makes sense to) be decoupled from that should be injected to make it more flexible and reusable, making the call-site have control over the dependencies of the called method, thus inversion of control.
In this boilerplate we make extensive use of dependency injection in the app
, interfaces
and infra
layers, for example:
- Injecting the ORM classes in the repositories at the
infra
layer; - Injecting the repositories in the operations at the
app
layer; - Injecting the operations in the controllers at the
interfaces
layer.
We implement dependency injection using the Awilix library, that is very flexible and easy to use. The author of the library has three wonderful posts about dependency injection on Node and how to use Awilix, you can read them here: part 1, part 2 and part 3. For the injections on the controllers we use the Express Awilix adapter.
The base of our dependency injection is a design pattern called composition root, and in the boilerplate it sits on the root of our src
folder, in the src/container.js
file. That's where we'll define what each of our injected dependencies will return, you should edit this file according to the growth of your application, like adding new operations and repositories to be injected.