Open ericxinzhang opened 2 years ago
you can register value in container
const SomeToken:InjectionToken<string> = "MyToken";
class Service{
constructor(
@inject(ServiceA) private dependencyA: ServiceA,
@inject(ServiceA) private dependencyB: ServiceB,
@inject(SomeToken) runtimeValue: string,
)
}
container.registerInstance(SomeToken, "runtimeValue");
container.resolve(SomeToken); // runtimeValue
container.resolve(Service); // Service instance
https://github.com/microsoft/tsyringe#injecting-primitive-values-named-injection
you can register value in container
const SomeToken:InjectionToken<string> = "MyToken"; class Service{ constructor( @inject(ServiceA) private dependencyA: ServiceA, @inject(ServiceA) private dependencyB: ServiceB, @inject(SomeToken) runtimeValue: string, ) } container.registerInstance(SomeToken, "runtimeValue"); container.resolve(SomeToken); // runtimeValue container.resolve(Service); // Service instance
https://github.com/microsoft/tsyringe#injecting-primitive-values-named-injection
Thank you. Registering the runtime value at runtime just before resolving looks a bit weird but I reckon that's the best we can get.
I think that @autoInjectable()
allows for this kind of behavior (so you could do new OrchestrationService(runtimeParam)
), but it wouldn't work with container.resolve()
.
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:And then I can somehow resolve the instance using the runtime value:
I think I have two problems here:
Any suggestion / help will be really appreciated.