April 2018
Beginner
536 pages
13h 21m
English
InversifyJS is an IoC container for TypeScript applications. InversifyJS can be used to implement the dependency inversion principle.
To use InversifyJS, we need to install it using npm as follows:
npm install inversify reflect-metadata --save
We can then import some of the entities declared by inversify and reflect-metadata as follows:
import { Container, inject, injectable } from "inversify";
import "reflect-metadata";
The following code snippet adds an annotation to the Ninja class using the inject decorator:
interface Weapon { tryHit(fromDistance: number): boolean; } @injectable() class Katana implements Weapon { public tryHit(fromDistance: number) { return fromDistance <= 2; } } @injectable() class Ninja { public ...