I'm trying to add dependency injection to a plain Typescript project, found an npm package called inversify.
So looking at the examples I came across this code:
import { Container, injectable, inject } from "inversify";
@injectable()
class Katana {
public hit() {
return "cut!";
}
}
@injectable()
class Shuriken {
public throw() {
return "hit!";
}
}
@injectable()
class Ninja implements Ninja {
private _katana: Katana;
private _shuriken: Shuriken;
public constructor(katana: Katana, shuriken: Shuriken) {
this._katana = katana;
this._shuriken = shuriken;
}
public fight() { return this._katana.hit(); };
public sneak() { return this._shuriken.throw(); };
}
var container = new Container();
container.bind<Ninja>(Ninja).to(Ninja);
container.bind<Katana>(Katana).to(Katana);
container.bind<Shuriken>(Shuriken).to(Shuriken);
What does it mean for class Ninja to implement the class Ninja?
class Ninja implements Ninja {
On the example the container is binding the class to itself, is it related to that?
var container = new Container();
container.bind<Ninja>(Ninja).to(Ninja);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…