I'm using @launchtray/tsyringe (a fork of Microsoft tsyringe with async initialization support) which is quite handy.
I'd like to implement something like the following:
A orchestration service whose constructor accepts dependency services (can be injected using @inject decorator) and a runtime value:
@injectable()
class OrchestrationService {
constructor(
@inject(ServiceA) private dependencyA: ServiceA,
@inject(ServiceA) private dependencyB: ServiceB,
runtimeValue: string,
) {
this.init(runtimeValue);
}
}
And then I can somehow resolve the instance using a runtime value:
const orchestrationService = container.resolve<OrchestrationService>(runtimeValue);
I don't know all possible runtime values therefore cannot register them beforehand.
I am wondering if injecting runtime value is possible and how to achieve that.
I posted the same question in tsyringe github repo and Andrey Khapugin answered my question. It's not perfect but I reckon it's the best we can get.
https://github.com/microsoft/tsyringe/issues/193#issuecomment-1084181366