Hello,
I finally decided to migrate to au2 but I’m having some issue with the DI part.
In au1 I have an implementation of the command pattern I would like to keep. I like it because I can just import my command and execute it. No need to specify it in the @inject()/resolve. It’s convenient.
command interface/factory
import { Container, Factory } from "aurelia-framework";
export type FactoryType<T> = () => T;
export function getFactory<T>(className): () => T {
return Factory.of(className).get(Container.instance);
}
command sample
import { getFactory, ICommand } from "app/commands/ICommand";
export class ScrollToCommand implements ICommand {
public execute(element: number): void {
window.scrollTo(0, element);
}
}
export const scrollTo = getFactory<ScrollToCommand>(ScrollToCommand);
usage sample
import { scrollTo } from "app/commands/ui/scrollTo";
class ScrollTopStep {
public run(
_routingContext: NavigationInstruction,
next: Next
): Promise<void> {
scrollTo().execute(0);
return next();
}
}
In au 2 what I could achieve so far
command interface/factory
export const rootContainer = DI.createContainer();
export function getFactory<T>(className): () => T {
const factory = rootContainer.getFactory(className);
return () => factory.construct(rootContainer);
}
command sample
export class ScrollToCommand implements ICommand {
constructor(
readonly httpClient: HttpClient = rootContainer.get(HttpClient),
) {
}
public execute(element: number): void {
window.scrollTo(0, element);
}
}
export const scrollTo = getFactory<ScrollToCommand>(ScrollToCommand);
The usage is the same. It’s kinda working but seems a bit hacky to use the rootContainer const and the arrow function that is required also. Is that the way to go ?
Thank a lot for your help.