容器API
InversifyJS 容器提供了一些便利方法来帮助开发者处理多重注入和模糊绑定的情况。
容器配置项
defaultScope
默认采用的是瞬态(transient)
作用域,您可以在声明绑定的时候更改类型模式。
container.bind<Warrior>(TYPES.Warrior).to(Ninja).inSingletonScope();
container.bind<Warrior>(TYPES.Warrior).to(Ninja).inTransientScope();
2
您可以在初始化时通过容器配置项改变默认的作用域。
let container = new Container({ defaultScope: "Singleton" });
autoBindInjectable
该配置项的作用是启动@injectable()
在装饰类的时候的自动绑定;
let container = new Container({ autoBindInjectable: true });
container.isBound(Ninja); // returns false
container.get(Ninja); // returns a Ninja
container.isBound(Ninja); // returns true
2
3
4
若同样已经手动绑定,则以手动绑定为准。
let container = new Container({ autoBindInjectable: true });
container.bind(Ninja).to(Samurai);
container.get(Ninja); // returns a Samurai
2
3
skipBaseClassChecks
您可以通过配置该项来跳过对@injectable
属性基类的检查,如果@injectable
类继承(extend)了不受您控制的类(比如第三方类),那么这个方法就特别有用了。该项的默认值是false
。
let container = new Container({ skipBaseClassChecks: true });
注意C是大写字母
Container.merge(a: Container, b: Container)将两个容器进行合并:
@injectable()
class Ninja {
public name = "Ninja";
}
@injectable()
class Shuriken {
public name = "Shuriken";
}
let CHINA_EXPANSION_TYPES = {
Ninja: "Ninja",
Shuriken: "Shuriken"
};
let chinaExpansionContainer = new Container();
chinaExpansionContainer.bind<Ninja>(CHINA_EXPANSION_TYPES.Ninja).to(Ninja);
chinaExpansionContainer.bind<Shuriken>(CHINA_EXPANSION_TYPES.Shuriken).to(Shuriken);
@injectable()
class Samurai {
public name = "Samurai";
}
@injectable()
class Katana {
public name = "Katana";
}
let JAPAN_EXPANSION_TYPES = {
Katana: "Katana",
Samurai: "Samurai"
};
let japanExpansionContainer = new Container();
japanExpansionContainer.bind<Samurai>(JAPAN_EXPANSION_TYPES.Samurai).to(Samurai);
japanExpansionContainer.bind<Katana>(JAPAN_EXPANSION_TYPES.Katana).to(Katana);
let gameContainer = Container.merge(chinaExpansionContainer, japanExpansionContainer);
expect(gameContainer.get<Ninja>(CHINA_EXPANSION_TYPES.Ninja).name).to.eql("Ninja");
expect(gameContainer.get<Shuriken>(CHINA_EXPANSION_TYPES.Shuriken).name).to.eql("Shuriken");
expect(gameContainer.get<Samurai>(JAPAN_EXPANSION_TYPES.Samurai).name).to.eql("Samurai");
expect(gameContainer.get<Katana>(JAPAN_EXPANSION_TYPES.Katana).name).to.eql("Katana");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
container.getNamed()
具名绑定:
let container = new Container();
container.bind<Weapon>("Weapon").to(Katana).whenTargetNamed("japanese");
container.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("chinese");
let katana = container.getNamed<Weapon>("Weapon", "japanese");
let shuriken = container.getNamed<Weapon>("Weapon", "chinese");
2
3
4
5
6
container.getTagged()
带标签绑定
let container = new Container();
container.bind<Weapon>("Weapon").to(Katana).whenTargetTagged("faction", "samurai");
container.bind<Weapon>("Weapon").to(Shuriken).whenTargetTagged("faction", "ninja");
let katana = container.getTagged<Weapon>("Weapon", "faction", "samurai");
let shuriken = container.getTagged<Weapon>("Weapon", "faction", "ninja");
2
3
4
5
6
container.getAll()
根据传入的标识符,返回所有可用的绑定
let container = new Container();
container.bind<Weapon>("Weapon").to(Katana);
container.bind<Weapon>("Weapon").to(Shuriken);
let weapons = container.getAll<Weapon>("Weapon"); // returns Weapon[]
2
3
4
5
container.getAllNamed()
根据传入的标识符,返回所有可用的具名绑定,这些绑定符合设定的限制条件:
let container = new Container();
interface Intl {
hello?: string;
goodbye?: string;
}
container.bind<Intl>("Intl").toConstantValue({ hello: "bonjour" }).whenTargetNamed("fr");
container.bind<Intl>("Intl").toConstantValue({ goodbye: "au revoir" }).whenTargetNamed("fr");
container.bind<Intl>("Intl").toConstantValue({ hello: "hola" }).whenTargetNamed("es");
container.bind<Intl>("Intl").toConstantValue({ goodbye: "adios" }).whenTargetNamed("es");
let fr = container.getAllNamed<Intl>("Intl", "fr");
expect(fr.length).to.eql(2);
expect(fr[0].hello).to.eql("bonjour");
expect(fr[1].goodbye).to.eql("au revoir");
let es = container.getAllNamed<Intl>("Intl", "es");
expect(es.length).to.eql(2);
expect(es[0].hello).to.eql("hola");
expect(es[1].goodbye).to.eql("adios");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
container.getAllTagged()
根据传入的标识符,返回所有可用的带标签绑定,这些绑定符合设定的限制条件:
let container = new Container();
interface Intl {
hello?: string;
goodbye?: string;
}
container.bind<Intl>("Intl").toConstantValue({ hello: "bonjour" }).whenTargetTagged("lang", "fr");
container.bind<Intl>("Intl").toConstantValue({ goodbye: "au revoir" }).whenTargetTagged("lang", "fr");
container.bind<Intl>("Intl").toConstantValue({ hello: "hola" }).whenTargetTagged("lang", "es");
container.bind<Intl>("Intl").toConstantValue({ goodbye: "adios" }).whenTargetTagged("lang", "es");
let fr = container.getAllTagged<Intl>("Intl", "lang", "fr");
expect(fr.length).to.eql(2);
expect(fr[0].hello).to.eql("bonjour");
expect(fr[1].goodbye).to.eql("au revoir");
let es = container.getAllTagged<Intl>("Intl", "lang", "es");
expect(es.length).to.eql(2);
expect(es[0].hello).to.eql("hola");
expect(es[1].goodbye).to.eql("adios");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
container.isBound(serviceIdentifier: ServiceIdentifier)
当我们想要检查一下容器是否已经注册了传入的绑定的时候,请使用isBound
方法。
interface Warrior {}
let warriorId = "Warrior";
let warriorSymbol = Symbol.for("Warrior");
@injectable()
class Ninja implements Warrior {}
interface Katana {}
let katanaId = "Katana";
let katanaSymbol = Symbol.for("Katana");
@injectable()
class Katana implements Katana {}
let container = new Container();
container.bind<Warrior>(Ninja).to(Ninja);
container.bind<Warrior>(warriorId).to(Ninja);
container.bind<Warrior>(warriorSymbol).to(Ninja);
expect(container.isBound(Ninja)).to.eql(true);
expect(container.isBound(warriorId)).to.eql(true);
expect(container.isBound(warriorSymbol)).to.eql(true);
expect(container.isBound(Katana)).to.eql(false);
expect(container.isBound(katanaId)).to.eql(false);
expect(container.isBound(katanaSymbol)).to.eql(false);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
container.isBoundNamed(serviceIdentifier: ServiceIdentifier, named: string)
您可以使用isBoundNamed
方法来检查容器是否已经绑定了传入的具名绑定。
const zero = "Zero";
const invalidDivisor = "InvalidDivisor";
const validDivisor = "ValidDivisor";
let container = new Container();
expect(container.isBound(zero)).to.eql(false);
container.bind<number>(zero).toConstantValue(0);
expect(container.isBound(zero)).to.eql(true);
container.unbindAll();
expect(container.isBound(zero)).to.eql(false);
container.bind<number>(zero).toConstantValue(0).whenTargetNamed(invalidDivisor);
expect(container.isBoundNamed(zero, invalidDivisor)).to.eql(true);
expect(container.isBoundNamed(zero, validDivisor)).to.eql(false);
container.bind<number>(zero).toConstantValue(1).whenTargetNamed(validDivisor);
expect(container.isBoundNamed(zero, invalidDivisor)).to.eql(true);
expect(container.isBoundNamed(zero, validDivisor)).to.eql(true);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
container.isBoundTagged(serviceIdentifier: ServiceIdentifier, key: string, value: any)
您可以使用isBoundTagged
方法来检查容器是否已经绑定了传入的带标签绑定。
const zero = "Zero";
const isValidDivisor = "IsValidDivisor";
let container = new Container();
expect(container.isBound(zero)).to.eql(false);
container.bind<number>(zero).toConstantValue(0);
expect(container.isBound(zero)).to.eql(true);
container.unbindAll();
expect(container.isBound(zero)).to.eql(false);
container.bind<number>(zero).toConstantValue(0).whenTargetTagged(isValidDivisor, false);
expect(container.isBoundTagged(zero, isValidDivisor, false)).to.eql(true);
expect(container.isBoundTagged(zero, isValidDivisor, true)).to.eql(false);
container.bind<number>(zero).toConstantValue(1).whenTargetTagged(isValidDivisor, true);
expect(container.isBoundTagged(zero, isValidDivisor, false)).to.eql(true);
expect(container.isBoundTagged(zero, isValidDivisor, true)).to.eql(true);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
container.rebind(serviceIdentifier: ServiceIdentifier)
您可以使用rebind
方法来用传入的服务标识符替换掉所有现有的绑定。该方法会返回一个BindingToSyntax
实例,从而方便您创建一个替代绑定。
let TYPES = {
someType: "someType"
};
let container = new Container();
container.bind<number>(TYPES.someType).toConstantValue(1);
container.bind<number>(TYPES.someType).toConstantValue(2);
let values1 = container.getAll(TYPES.someType);
expect(values1[0]).to.eq(1);
expect(values1[1]).to.eq(2);
container.rebind<number>(TYPES.someType).toConstantValue(3);
let values2 = container.getAll(TYPES.someType);
expect(values2[0]).to.eq(3);
expect(values2[1]).to.eq(undefined);
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
container.resolve(constructor: Newable)
该方法和container.get<T>(serviceIdentifier: ServiceIdentifier<T>)
相似,但它允许用户在未声明绑定的情况下创建实例:
@injectable()
class Katana {
public hit() {
return "cut!";
}
}
@injectable()
class Ninja implements Ninja {
public katana: Katana;
public constructor(katana: Katana) {
this.katana = katana;
}
public fight() { return this.katana.hit(); }
}
const container = new Container();
container.bind(Katana).toSelf();
const tryGet = () => container.get(Ninja);
expect(tryGet).to.throw("No matching bindings found for serviceIdentifier: Ninja");
const ninja = container.resolve(Ninja);
expect(ninja.fight()).to.eql("cut!");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
请注意:它仅允许跳过在依赖的根元素一级的声明。所有的子依赖项(例如前面示例中的Katana
)的绑定仍需要声明。
← 对Symbol的支持 声明容器模块 →