-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Introduce handle method on MessageBusInterface instead of Trait #29428
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
Comments
I have been struggling with the same problem. I thought the MessageBus was a great way to implement queries. I just found out today that I need to use a Trait now. |
@luzivenom You're not forced to use the Trait, you could implement the logic yourself. For instance, you could do something like this (Which is similar to how it is implemented at the moment) $envelope = $this->queryBus->dispatch($message);
// Needs some additional checking of course
// https://github.com/symfony/messenger/blob/master/HandleTrait.php
$result = $envelope->last(HandledStamp::class)->getResult(); I just think that it's a step backwards from previously being able to do $result = $this->queryBus->dispatch($message); which could now simply become $result = $this->queryBus->handle($message); |
Regarding points 2 and 3 (and maybe 1 to) if think your are not supposed to use the trait inside controllers, instead you should create your own |
@jvasseur Agreed, I do not want to use Traits in my Controllers (and you should not, it was just an example if I don't want to implement my own Bus). |
@jvasseur Idk about creating my own Bus. I agree with @hacked that it seems odd to create my own bus which uses another bus to do what my framework should be doing in the first place. I saw that @ogizanagi mentioned in #29167 that he wants to add a base class. Would that help with this? |
I don't disagree that it makes things a bit more complex. Personally I'm in favor of always decorating the MessageBus with my own class (even for command busses). It allow adding parameters (and return types in case of query buses) type, allow decorating the messages with envelopes instead of having to do it in controllers and allow using auto-wiring when working with multiple bus instances. The problem with letting the framework do everything is that Message buses can be used in a lot of cases and the framework can't really know how you expect it to work so encouraging people to decorate it looks like a good trade-of. |
@jvasseur got it 100% right. The job of a framework is to encourage maintainable practices. Having a public method that takes any object and that returns an unspecified value with a pluggable implementation doesn't belong to that description. What the trait provides is an implementation that may help implement a query/command bus - you can copy/paste it if the exact details of its implementation don't fit your style. And in any way, in you code, you should really declare both a more specific argument type AND return type. The component cannot help here, only you know what to declare. |
Closing as explained. |
Description
This issue is related to the PR #29167 and #28983, which I believe does not solve the recent changes in the Messenger component.
There should be a
handle
method on theMessageBusInterface
itself, which I believe is a better solution than introducing a Trait. Using said Trait comes with a set of limitations that I believe can be solved by adding previous functionality back to theMessageBusInterface
.$messageBus
as the property name, before you were able to name it to your liking, such as$commandBus
or$queryBus
, which makes more sense from a semantical point of view.handle()
method of the Trait.MessageBusInterface
, for instance in a Controller, which then also needs to useHandleTrait
. It should be as simple as calling$this->queryBus->handle(new Query());
(Edit: Alternatively I have to create my own QueryBus that uses the
HandleTrait
, which should not be needed since my Framework already offers a working Bus and also causes confusion where that Trait should actually be used.)I understand the issue with return types, as discussed in the PR mentioned above, however the current implementation seems to be too focused on solving a specific issue, which might not need to be solved at all.
Regarding the explanation of implementing your own QueryBus from this PR, again, I believe this is going out of the way of how things should work. I think sacrificing not having a return type on the
handle
method is an acceptable trade-off here.Example
I propose to (re)introduce a
handle()
method on theMessageBusInterface
, which will solve the issues mentioned above, as well as make it simpler to use. TheHandleTrait
can be removed again, and the logic from within thehandle
method would be moved back into theMessageBus
implementation.The text was updated successfully, but these errors were encountered: