Mark Garratt

Mark Garratt

Who am I?

Web Developer based in London, UK


What I write about


Recent Posts

Understanding the Command Bus

Recently I have been trying to understand the concept of a command bus and taking a command driven approach to developement. It has been spoken about it the Laravel community for a long time and is now supported out-of-the-box in Laravel 5.

The way it works is you create a command object, pass it into the bus which finds the correct handler and executes the command. I like this approach because it provides a clean way to interact with your domain model and the command objects are named in a way that conveys intent. There is a huge amount of information on the Internet around the use of a command bus, especially in Laravel, so I'm not going to go into the details of implementation in this post.

Whilst researching this concept the one thing I found missing was how to deal with response data. Every post seems to start at the point where you create your command object and then go into the details of busses and handlers, but how do you create the response to the user? A command bus does not return any response, you place the command onto the bus and it is handled, there is nothing more to do.

Taking a typical example that will apply to most systems, registering a user, you will normally see some example code like this (properties, getters, setters, etc. ommitted):

# RegisterUserCommand.php
<?php

class RegisterUserCommand
{
    public function __construct($username, $email)
    {
        $this->username = $username;
        $this->email = $email;
    }
}
# RegisterUserHandler.php
<?php

class RegisterUserHandler
{
    public function handle(RegisterUserCommand $command)
    {
        // Create the member, fire events, etc. (not all in this method)

        // Throw domain specific exceptions if anything goes wrong
    }
}
# UserController.php
<?php

class UserController
{
    private $bus;

    public function __construct(CommandBus $bus, UserRepository $userRepository)
    {
        $this->bus = $bus
    }

    public function register(Request $request)
    {
        // Validate here if necessary
        $username = $request->get('username');
        $email = $request->get('email');

        // Create and execute the command
        $command = new RegisterUserCommand($username, $email);
        $this->bus->execute($command);

        // This is the happy path, assume the command was successful at this point
        $newUser = $this->userRepository->findByEmail($email);

        return response()->json($newUser);
    }
}

I have created an example project on GitHub here. The core concept I feel is missing from other examples is the lines under $this->bus->execute(...); where the newly created user is retrieved from the repository and returned in the response. If there were any errors they would have been dealt with by the command handler throwing an exception and that being caught by the Laravel exception handler.

Hopefully this post can provide an answer to the question I was struggling with when learning about the Command Bus "How to return a response?" Going further than this there is the consideration of how this might work in an environment different to PHP where the command bus is asynchronous, how would you then get the confirmation from the command? My initial opinion on this is to use events, but I haven't researched it properly and it isn't really in the scope of this post.