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
[Messenger] Add Envelope::getResult()
#49294
base: 6.3
Are you sure you want to change the base?
Conversation
Envelope::getResult()
We already have https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Messenger/HandleTrait.php which kind of achieves the same goal as your proposed method, but it doesn't play nice with invokable controllers so I personally avoid using it. In the docs: https://symfony.com/doc/current/messenger/handler_results.html#working-with-command-query-buses, it is used like this: class ListItems
{
use HandleTrait;
public function __construct(MessageBusInterface $messageBus)
{
$this->messageBus = $messageBus;
}
public function __invoke()
{
$result = $this->handle(new ListItemsQuery(/* ... */));
// Do something with the result
// ...
}
} but most controllers these days look like this: class ListItemsController
{
use HandleTrait;
#[Route(path: '/', name: 'demo']
public function __invoke(
MessageBusInterface $messageBus
) {
$result = $this->handle(new ListItemsQuery(/* ... */));
// Do something with the result
// ...
}
} which doesn't work with the trait. I'd have to add an explicit constructor, which I don't want to do. I like moving the code from the trait into the |
We'd have to deprecate HandleTrait then. Not sure if it's worth it yet, personally. |
Where's the problem?EDIT:
So instead of calling |
Smart! But if the trait requires a separate "gateway" class with nothing but a constructor to be useful, doesn't this mean that the design/DX of the current solution could be improved? |
@dreadnip like everything - it depends. |
Personally, I use this pattern as it's easier to type-hint ctor of my handlers or controllers like in this example with a The idea can be extended for eg. with the expected return type and additional assertion on
AFAIK the Messenger component was designed with simplicity in mind so in general the use cases we discuss here IMHO should be a subject of a standalone component if ever. If you start thinking about separate buses for queries and commands and events you see that one simple |
im not sure most controllers these days use services as controller arguments |
I agree with @ro0NL , for eg. most if not all controllers look as follows:
|
I think we must find a solution to work with both , otherwise this ends up being a yes! No! Yes! No! discussion. I haven't used an invoke constructor for years for instance, and haven't seen one on symfony casts in a while. Not to say one is better.... But to prove both are valid and must be supported. |
we were reading https://thomas.jarrand.fr/blog/cache-query-avec-symfony-messenger/ and a line triggered @damienalexandre:
I agree this line is not very optimal:
That's why I propose a shortcut
If this feature is accepted, I'll write