Open 39otrebla opened 2 years ago
Im in a very similar situation. React Native, TypeScript and trying to inject a singleton interface implementation by constructor, but is not found
Looks like prevention from falling to Service Locator anti-pattern ;)
Can't seem to get this working even the README way...
If we have such a decorator:
export function Decorator<T>(): (target: constructor<T>) => any {
return (target: constructor<T>): void => {
singleton(target);
I woul've expected something like this to work:
export function Decorator<T>(): (target: constructor<T>) => any {
return (target: constructor<T>): void => {
singleton(delay(() => target));
any workarounds?
Was able to come up with.
export function delayInjection<T>(target: constructor<T>): T {
return delay(() => target).createProxy((ctor) => globalContainer.resolve(ctor));
}
class A {
private service: Service = delayInjection(Service);
Describe the bug Not sure whether this is a bug or I'm missing something, but surely it is a weird behavior (at least coming from other DI frameworks like Swift's
Resolver
).I can't find a way to register two Singletons and inject the first one into the second one. I did nothing different than what I see in the Readme's Interface example, except that I need those implementations to be Singletons, thus registered with
registerSingleton
(which, by the way, I don't see documented in the Readme).I also made an attempt with the following, with no luck:
Everything works perfectly using the exact same code but resolving without decorators:
To Reproduce Setup a ReactNative project with Typescript, following the official ReactNative doc. Also, follow the Tsyringe Getting Started to add required dependencies and TS configs (
reflect-metadata, etc, etc
). Then try the following:Expected behavior
resolve()
call, and that instance should be "cached" in the containerresolve()
call, aand that instance should be "cached" in the containerresolve()
call happens to be the injection into a class constructor, it should workVersion: 4.6.0