You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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:
//////////// index.jsimport"reflect-metadata";import"./ioc/setup.ts";//////////// /ioc/setup.tsimport{container}from"tsyringe";import{LoggerServiceName,RouterServiceName}from"./registry.ts";import*asServicesfrom"@ports";// index for all services/portsimport*asAdaptersfrom"@adapters";// index for all adaptersimport{T1,T2}from"my-navigation-library";container.registerSingleton<Services.LoggerService>(LoggerServiceName,Adapters.LoggerAdapter,);container.registerSingleton<Services.RouterService<T1,T2>>(RouterServiceName,Adapters.RouterAdapter,);//////////// ioc/registry.tsexportconstLoggerServiceName="LoggerService";exportconstRouterServiceName="RouterService";//////////// adapters/router.adapter.tsimport{LoggerService,RouterService}from"@ports";import{LoggerServiceName}from"@registry";import{T1,T2}from"my-navigation-library";
@injectable()// I also did an attempt with @authInjectable(), no luckexportclassRouterAdapterimplementsRouterService<T1,T2>{constructor(@inject(LoggerServiceName)privatelogger?: LoggerService){}[...]}//////////// adapters/logger.adapter.tsimport{LoggerService}from"@ports";exportclassLoggerAdapterimplementsLoggerService{[...]}//////////// app/firstScreen.jsimport{container}from"tsyringe";import{RouterServiceName}from"@registry";someFunction(){constrouter=container.resolve(RouterServiceName);// throws the following// Cannot inject the dependency at position #0 of "RouterAdapter" constructor. Reason: Attempted to construct an undefined constructor. Could mean a circular dependency problem. Try using `delay` function.}
Expected behavior
RouterAdapter gets instantiated upon first resolve() call, and that instance should be "cached" in the container
LoggerAdapter gets instantiated upon first resolve() call, aand that instance should be "cached" in the container
If the first resolve() call happens to be the injection into a class constructor, it should work
Version:
4.6.0
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: