Skip to content

Using a CommandHandler #1

@josecelano

Description

@josecelano

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions