ddd-sample-symfony icon indicating copy to clipboard operation
ddd-sample-symfony copied to clipboard

Using a CommandHandler

Open josecelano opened this issue 10 years ago • 3 comments

In your controller:

https://github.com/tyx/ddd-sample-symfony/blob/master/src/BookingEngine/UI/Controller/BookingController.php#L50

        $form->handleRequest($request);
        if (!$form->isValid()) {
            throw new HttpException(406, (string) new FormErrorsRepresentation($form->getErrors()));
        }
        try {
            // Send the message
            $this->bookingService->payBooking($command);

You use a service and pass a command as parameter.

This other Symfony DDD exmaple uses a CommandHandler:

https://github.com/leopro/trip-planner/blob/master/src/Leopro/TripPlanner/PresentationBundle/Controller/ApiController.php#L27

        $form->handleRequest($request);
        if ($form->isValid()) {
            $trip = $this->get('command_handler')->execute($form->getData());
            return new Response('ok');
        }

In that sample payBooking would use a Use Case.

Prons:

  • Common logic in Command execution as validation and event dispatcher.
  • Avoid fat services, your bookingService would be replace with some UseCase classes.

This other sample uses commandBus instead of CommandHandler:

https://github.com/tyx/cqrs-php-sandbox/blob/master/src/Afsy/UI/Controller/GameCommandController.php

I am trying to figure out which is the best option to implement DDD in a project with Symfony. Perpahs a mixing of them or it depends on the domain. What do you think?

josecelano avatar Jan 09 '15 22:01 josecelano

Hi Jose !

I'm of course in favor of using Command Bus!
I just wanted to show that we are not required to use a real command bus class to start using Command Pattern. And it is more important to use a concept that the implementation of this concept ;)

In order to move some legacy code, it could make sense sometimes to not follow full recommendations as the goal is to simplify the comprehension and the reading of your legacy code.

But for a new project or a project with dedicated time to improve, I would add a real commandbus of course.

Avoid fat services, your bookingService would be replace with some UseCase classes.

But I disagree with this part. BookingService is an application service not a domain service. No problem to make it fat. I prefer big application service that make sense for a big Bounded Context, that many and many Command Handler class, totally separate. That help to think in Bounded Context IMO.

Again, we are talking about details, the main move here is to move to the Command Pattern, whatever the way to get there.

Hope it helps.

tyx avatar Jan 12 '15 08:01 tyx

Thanks Timothée.

I am trying to find the best way to implement a MessageBus in Symfony 2. I have found these two repositories:

https://github.com/SimpleBus https://github.com/beberlei/litecqrs-php#litecqrs-for-php

I would like to find an Symfony example application with one of them.

josecelano avatar Jan 13 '15 13:01 josecelano

I use litecqrs on my other sandbox : https://github.com/tyx/cqrs-php-sandbox/blob/master/src/Afsy/UI/SymfonyBundle/Resources/config/commands.yml

For now, it is the best I see, but Benjamin is no more very active on it : (

I don't give a try to the other ones that pop the last monthes.

tyx avatar Jan 13 '15 14:01 tyx