# \[AU2\] Trouble migrating commands using DI

**URL:** <https://discourse.aurelia.io/t/au2-trouble-migrating-commands-using-di/6350>\
**Category:** Help Requests\
**Created:** [February 3, 2025, 11:38pm UTC](https://discourse.aurelia.io/t/au2-trouble-migrating-commands-using-di/6350 "2025-02-03T23:38:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cedric-vermeulen](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/cedric-vermeulen/32/1930_2.png) [@cedric-vermeulen](https://discourse.aurelia.io/u/cedric-vermeulen)\
**Post date:** [February 3, 2025, 11:38pm UTC](https://discourse.aurelia.io/t/au2-trouble-migrating-commands-using-di/6350/1 "2025-02-03T23:38:44Z")

</div>

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

```auto
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

```auto
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

```auto
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

```auto
export const rootContainer = DI.createContainer();

export function getFactory<T>(className): () => T {
   const factory = rootContainer.getFactory(className);
   return () => factory.construct(rootContainer);
}

```

command sample

```auto

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.

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [February 4, 2025, 7:47pm UTC](https://discourse.aurelia.io/t/au2-trouble-migrating-commands-using-di/6350/2 "2025-02-04T19:47:57Z")

</div>

You may create an injectable factory for that, if you want.

```ts
import { DI, resolve } from '@aurelia/kernel';

export const IMyFactory = DI.createInterface<IMyFactory>(
  'IMyFactory', 
  x => x.singleton(MyFactory)
);
export interface IMyFactory extends MyFactory {}

export class MyFactory {
  private readonly container: IContainer = resolve(IContainer);
  public create($class: Class<unknown>) { return this.container.invoke($class); }
}

```

Then, you can simply inject your factory.

```ts
class ScrollTopStep {
  private readonly factory: IMyFactroy = resolve(IMyFactroy);
  public run(
    _routingContext: NavigationInstruction,
    next: Next
  ): Promise<void> {
    this.factory.create(ScrollToCommand).execute(0);

    return next();
  }
}

```

---

<div class="post-metadata">

**Author:** ![cedric-vermeulen](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/cedric-vermeulen/32/1930_2.png) [@cedric-vermeulen](https://discourse.aurelia.io/u/cedric-vermeulen)\
**Post date:** [February 4, 2025, 9:12pm UTC](https://discourse.aurelia.io/t/au2-trouble-migrating-commands-using-di/6350/3 "2025-02-04T21:12:41Z")

</div>

Thanks for the suggestion !

I like the idea. However in my case I like the command to be callable directly from the import like in my sample. But maybe i can combine both. I mean exposing a const from inside the command definition so it can be called like this:

```auto
// no injection required for this to work
scrollTo().execute(0);

```
