lor*_*non 4 dependency-injection typescript inversifyjs
在InversifyJS中,遵循工厂注入指南和构造函数注入指南中概述的方法,分别仅使用toDynamicValue注入工厂和构造器,是否有任何特定的优点.
Rem*_*sen 14
如果使用a,toConstructor您将能够将参数传递给构造函数,但是您将无法解析这些参数(除非您也注入它们).
container.bind<interfaces.Newable<Katana>>("Newable<Katana>")
.toConstructor<Katana>(Katana);
Run Code Online (Sandbox Code Playgroud)
如果你使用a,toDynamicValue你将能够将构造函数参数传递给构造函数并使用context.
container.bind<Katana>("Katana")
.toDynamicValue((context: interfaces.Context) => {
return new Katana(context.container.get("SomeDependency"));
});
Run Code Online (Sandbox Code Playgroud)
如果使用a,toFactory您将能够将构造函数参数传递给构造函数并使用它来解析这些参数,context但您也可以根据工厂参数生成不同的输出.
container.bind<interfaces.Factory<Weapon>>("Factory<Weapon>")
.toFactory<Weapon>((context: interfaces.Context) => {
return (throwable: boolean) => {
if (throwable) {
return context.container.getTagged<Weapon>(
"Weapon", "throwable", true
);
} else {
return context.container.getTagged<Weapon>(
"Weapon", "throwable", false
);
}
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2325 次 |
| 最近记录: |