Open amura11 opened 3 years ago
Hello and thanks for your question, if you want to pass the same baseUrl
to all the instances of your service I think the good practise (in inversify) is to use a Factory https://github.com/inversify/InversifyJS/blob/master/wiki/factory_injection.md
If you want to have different baseUrl
for each instance I usually create another method instead of passing to the constructor.
Please confirm, that this helped you and have a nice day.
Would it be possible to extend the container with something like:
class CustomContainer extends Container {
public addDynamicTransient<T>(constructor: Constructor<T>, func: () => T): interfaces.BindingWhenOnSyntax<T> {
const id = generateIdAndAddToCache(constructor);
this.decorateCatchable(injectable(), constructor);
return super.bind<T>(id).toDynamicValue(func).inTransientScope();
}
}
const container = new CustomContainer();
container.addDynamicTransient<IMyService>(MyService, () => new MyService("BaseUrl"));
I tried exactly this but I get an error that no bindings were found
Why are you extending the container and not using as factory?
Assuming I have the following:
Is there a way to pass
baseUrl
when registeringMyService
with the container? Something like:container.addTransient<IMyService>(() => new MyService("BaseUrl"));
? Coming from C# this is typically how it's done so I might just be approaching this completely wrong.I want to set
baseUrl
at the time of registering as it will change depending on the environment.