Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup DI container, when workflow needs the bus instance #193

Closed
mathiasbn opened this issue Feb 2, 2023 · 2 comments
Closed

How to setup DI container, when workflow needs the bus instance #193

mathiasbn opened this issue Feb 2, 2023 · 2 comments

Comments

@mathiasbn
Copy link

From the example workflow in the guide. A normal thing would be to have a workflow requiring a BusInstance:

export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {

  constructor (bus: BusInstance) {}

In order to setup dependency injection the examples show:

const container = new Container()

  const bus = await Bus.configure()
    .withContainer({
      get <T>(type: ClassConstructor<T>) {
        return container.get<T>(type)
      }
    })
    .initialize()

Now I 'would' have i BusInstance and can add it to the DI container.
The problem is, that initialize tries to create all workflows using the DI container.

So it will try to construct FulfulmentWorkflow, and to do that, the DI container will try to get the BusInstance, but this is not added yet.
And cannot be added, because we still don't have the bus instance...

So as I can see it, we are in a vicious circle :)

We are updating from 1.0.0. This seems to be added with
Use container to get workflows: #169

Am I completely missing something? (wouldn't be the first time)

@adenhertog
Copy link
Contributor

hi @mathiasbn - what you're running into is not uncommon. The .initialize() step requests instances of each workflow from your DI container in order to determine what messages are consumed so it can configure queues and subscriptions. During this step, your workflow is never run - just the configureWorkflow function is executed.

One way to ensure that the BusInstance is available when a message eventually activates your workflow is to dynamically/late bind it. I've provided an example at https://github.com/node-ts/examples/blob/basic-example/src/basic-workflow/index.ts#L21 that uses an Inversify container, but any reasonable mature DI provider should give you the same ability.

Hope this helps.

@mathiasbn
Copy link
Author

Hi again. Thank you for responding, and I'm sorry I totally forgot to answer.
That helped me on, I still find it a little tricky, but there is probably a good reason.
Anyways this can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants