inversify-token
简介
基于token的InversifyJS层,为TypeScript注入提供了更强大的类型安全保证。
安装
npm i inversify-token
1
使用
// 声明你的接口.
// 文件📃 types.ts
import { Token, TokenType } from "inversify-token";
import { Warrior, Weapon, ThrowableWeapon } from "./interfaces"
const WarriorToken = new Token<Warrior>(Symbol.for("Warrior"));
type WarriorToken = TokenType<typeof WarriorToken>;
const WeaponToken = new Token<Weapon>(Symbol.for("Weapon"));
type WeaponToken = TokenType<typeof WeaponToken>;
const ThrowableWeaponToken = new Token<ThrowableWeapon>(Symbol.for("ThrowableWeapon"));
type ThrowableWeaponToken = TokenType<typeof ThrowableWeaponToken>;
export {
WarriorToken as Warrior,
WeaponToken as Weapon,
ThrowableWeaponToken as ThrowableWeapon,
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 文件📃 entities.ts
import { injectable } from "inversify";
import { injectToken } from "inversify-token";
import * as TYPES from "./types";
@injectable()
class Ninja implements Warrior {
public constructor(
@injectToken(TYPES.Weapon) private _katana: TYPES.Weapon,
@injectToken(TYPES.ThrowableWeapon) private _shuriken: TYPES.ThrowableWeapon,
) { }
public fight() { return this._katana.hit(); }
public sneak() { return this._shuriken.throw(); }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 文件📃 inversify.config.ts
import { getToken, tokenBinder } from "inversify-token";
const myContainer = new Container();
const bindToken = tokenBinder(myContainer.bind.bind(myContainer));
bindToken(TYPES.Warrior).to(Ninja);
bindToken(TYPES.Weapon).to(Katana);
bindToken(TYPES.ThrowableWeapon).to(Shuriken);
const warrior = getToken(container, TYPES.Warrior);
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
// 文件📃 inversify.module.ts
import { getToken, TokenContainerModule } from "inversify-token";
const myContainer = new Container();
const module = new TokenContainerModule((bindToken) => {
bindToken(TYPES.Warrior).to(Ninja);
bindToken(TYPES.Weapon).to(Katana);
bindToken(TYPES.ThrowableWeapon).to(Shuriken);
});
myContainer.load(module);
const warrior = getToken(container, TYPES.Warrior);
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11