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
Question: [typed-inject in Clean Architecture] How to inject instances so that other registered instances can reference them while being used as injectables themselves by other instances?
#59
Open
haukehem opened this issue
Apr 23, 2023
· 1 comment
Hi! First of all, I really appreciate this project and it's type-safe DI approach to Typescript. However, while implementing a little demonstration project with a Clean Architecture approach, I could not figure out how to correctly set up my IoC container.
My question sounds a bit long-winded, but let my try to describe my approach.
Short description of my object relation / call flow:
UI/reducer -> Use-Case -> Service (impl. by a Repository) -> Datasource -> API client
To minimize the amount dependencies, increase performance and overall be more flexible/adaptable, in my UI, I want to get the injected Use-Case instance, which in itself references a Service registered in the IoC and so on...
Now, to my approach. I started by implementing my API client:
exportclassTypes{// ClientsstaticapiClient="apiClient";// Data sourcesstaticiAccountApiDatasource="iAccountApiDatasource";staticiAccountLocalDatasource="iAccountLocalDatasource";staticiAccountSessionDatasource="iAccountSessionDatasource";
...
// ServicesstaticauthenticationService="authenticationService";
...
// Use casesstaticsignIn="signIn";
...
}// Called in index.tsxexportfunctioninitializeDependencies(){returncreateInjector().provideClass(Types.apiClient,BlueprintOffersApiClient,Scope.Singleton).provideClass(Types.iAccountApiDatasource,AccountApiDatasource,Scope.Singleton).provideClass(Types.iAccountLocalDatasource,AccountLocalDatasource,Scope.Singleton).provideClass(Types.iAccountSessionDatasource,AccountSessionDatasource,Scope.Singleton).provideClass(Types.authenticationService,AuthenticationRepository,Scope.Singleton).provideClass(Types.signIn,SignIn,Scope.Singleton);}
But with this setup, the compiler displays the following error in the line providing the AuthenticationRepository: TS2345: Argument of type 'typeof AuthenticationRepository' is not assignable to parameter of type 'InjectableClass {}, BlueprintOffersApiClient, string>, AccountApiDatasource, string>, AccountLocalDatasource, string>, AccountSessionDatasource, string>, AuthenticationRepository, readonly [...]>'.
So, to get back to my (multipart) question:
What is the error trying to tell me and and why does it occur - I really want to understand it
How can I inject my API client(s), Datasources, Repositories and Use-Cases instances so that other registered instances can reference them while being used as injectables themselves by "higher" (closer to UI) instances? - But I also need a solution:D
The text was updated successfully, but these errors were encountered:
After trying out some things, I could get rid of the compiler errors in my IoC by instanciating all dependencies and wiring the stuff manually:
exportfunctioninitializeDependencies(){constcontainer=createInjector().provideClass(Types.apiClient,BlueprintOffersApiClient);// Data sourcesconstaccountApiDatasource=newAccountApiDatasource(container.resolve(Types.apiClient))container.provideValue(Types.iAccountApiDatasource,accountApiDatasource);constaccountLocalDatasource=newAccountLocalDatasource();container.provideValue(Types.iAccountLocalDatasource,accountLocalDatasource);constaccountSessionDatasource=newAccountSessionDatasource();container.provideValue(Types.iAccountSessionDatasource,accountSessionDatasource);// RepositoriesconstauthenticationRepository=newAuthenticationRepository(accountApiDatasource,accountLocalDatasource,accountSessionDatasource)container.provideValue(Types.authenticationService,authenticationRepository);// Use-CasesconstsignIn=newSignIn(authenticationRepository);container.provideValue(Types.signIn,signIn);returncontainer;}
But not only is this a pretty strange solutin on its own. Now, when I try to resolve a dependency (here: Calling SignIn-Use-Case in UI), the type of the instance I want to resolve is interpretated as BlueprintOffersApiClient, my API client implementation:
Hi! First of all, I really appreciate this project and it's type-safe DI approach to Typescript. However, while implementing a little demonstration project with a Clean Architecture approach, I could not figure out how to correctly set up my IoC container.
My question sounds a bit long-winded, but let my try to describe my approach.
Short description of my object relation / call flow:
UI/reducer -> Use-Case -> Service (impl. by a Repository) -> Datasource -> API client
To minimize the amount dependencies, increase performance and overall be more flexible/adaptable, in my UI, I want to get the injected Use-Case instance, which in itself references a Service registered in the IoC and so on...
Now, to my approach. I started by implementing my API client:
Next, my API Datasource of my Account module:
In my Account module, I have two other Datasource NOT referencing the API client or any other thing:
The Datasources are used in the module's Repositories (Service implementations):
The Use-Cases then reference the Services:
Ok, so far so good. Now I want to set up my IoC:
But with this setup, the compiler displays the following error in the line providing the
AuthenticationRepository
:TS2345: Argument of type 'typeof AuthenticationRepository' is not assignable to parameter of type 'InjectableClass {}, BlueprintOffersApiClient, string>, AccountApiDatasource, string>, AccountLocalDatasource, string>, AccountSessionDatasource, string>, AuthenticationRepository, readonly [...]>'.
So, to get back to my (multipart) question:
The text was updated successfully, but these errors were encountered: