Open haukehem opened 1 year ago
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:
export function initializeDependencies() {
const container = createInjector().provideClass(Types.apiClient, BlueprintOffersApiClient);
// Data sources
const accountApiDatasource = new AccountApiDatasource(container.resolve(Types.apiClient))
container.provideValue(Types.iAccountApiDatasource, accountApiDatasource);
const accountLocalDatasource = new AccountLocalDatasource();
container.provideValue(Types.iAccountLocalDatasource, accountLocalDatasource);
const accountSessionDatasource = new AccountSessionDatasource();
container.provideValue(Types.iAccountSessionDatasource, accountSessionDatasource);
// Repositories
const authenticationRepository = new AuthenticationRepository(
accountApiDatasource,
accountLocalDatasource,
accountSessionDatasource
)
container.provideValue(Types.authenticationService, authenticationRepository);
// Use-Cases
const signIn = new SignIn(authenticationRepository);
container.provideValue(Types.signIn, signIn);
return container;
}
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:
export default function SignInViewModel() {
const signIn = container.resolve(Types.signIn);
async function onSignIn() {
await signIn.invoke();
}
...
}
The compiler throws the following error for await signIn.invoke();
:
TS2339: Property 'invoke' does not exist on type 'BlueprintOffersApiClient'.
Probably the way I tried it here is not the idea of the plugin. Just want to protocoll what I've tried and what errors are displayed.
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: