Constructor Injection
Inject dependencies through constructors cleanly.
Constructor Injection in Depth
Constructor injection declares every dependency as a constructor parameter. The object is fully wired the moment it exists, which makes required collaborators impossible to forget.
Parameter Properties
TypeScript can declare and assign a dependency in one step using an access modifier on the parameter. private readonly creates and assigns the field automatically.
interface Mailer { send(to: string, body: string): void; }
class Notifier {
constructor(private readonly mailer: Mailer) {}
}All lessons in this course
- Inversion of Control Basics
- Constructor Injection
- DI Containers with InversifyJS
- Type-Safe Service Tokens