vgaldikas's avatar

When working with CommandBus, whats the best approach in order to receive the outcome of the CommandHandler?

I try to follow this https://dev.to/mtk3d/restore-missing-command-bus-in-new-laravel-versions-4n65, so I got the CommandBus working.

But now curious, whats the best approach to be able to receive some answer from the CommandHandler?

Or should this pattern be only used for asynchronous jobs, where you don't care about the outcome immediately?

The only reasonable way I can think of is to have a Query service that you call after dispatching the command

0 likes
2 replies
martinbean's avatar

But now curious, whats the best approach to be able to receive some answer from the CommandHandler?

@vgaldikas You dispatch a command, the command is handled, and returns a response. If you want to return something, return it from your command’s handle method:

$result = $this->bus->dispatch(new SomeCommand($foo, $bar));

Or should this pattern be only used for asynchronous jobs, where you don't care about the outcome immediately?

It’s what powers queue jobs, but it’s absolutely not just for asynchronous code.

vgaldikas's avatar

@martinbean

I tried this, what I don't like with this part:

$result = $this->bus->dispatch(new SomeCommand($foo, $bar));

is that it doesn't seem to be possible to type hint the return value. Of course I can do it with annotation, but I would rather have it enforced at the language level.

So for now I went with a separate query service

Please or to participate in this conversation.