我需要使用 tsyringe 实现某种策略模式,但无法弄清楚如何在不污染依赖容器的情况下动态地将策略注入到我的上下文类中。
interface IThing {
exec(): void;
}
@injectable()
class Thing1 implements IThing {
exec(): void {
console.log('thing 1');
}
}
@injectable()
class Thing2 implements IThing {
exec(): void {
console.log('thing 2');
}
}
@injectable()
class Test {
constructor(
// how would the container know which is which between Thing1 and Thing2?
@inject('??')
private readonly thing: IThing,
) {
thing.exec();
}
}
// find a way to resolve `Test` and give it Thing1 or Thing2
我不确定如何使用
container.resolve(Test)
并给予它 Thing1
/Thing2
。
我知道我可以做如下的事情,但我不确定这是否非常优雅,因为它会污染容器并存在使用错误策略实例化
Test
的风险:
@injectable()
class Test {
constructor(
@inject('Thing')
private readonly thing: IThing,
) {
thing.exec();
}
}
container.provide("Thing", { useClass: Thing1/2 })
const test = container.resolve(Test) // test with proper strategy, but now the container is polluted
有没有更优雅的方式来实现这个模式?
我可以想到两种方法:
您可以删除
@inject('Thing')
课程上的 Test
。
选项 1 的示例:
const container1 = container.createChildContainer();
container1.register('IThing', {useClass: Thing1});
const test1 = container1.resolve(Test); // output : 'Thing 1'
const container2 = container.createChildContainer();
container2.register('IThing', {useClass: Thing2});
const test2 = container2.resolve(Test); // output : 'Thing 2'