Process Managers

Last updated at 23 March 2018 | Published at 07 March 2018

Projections and read models are a big part of event sourcing. They are a way to communicating state with the outside world. They’re also very project-specific.

In EventSauce process managers are nothing more than an implementation of the Consumer interface. Unlike projections, which feed read models, process managers do more than just respond to something that happened. Process managers act. How the process manager interacts with your system depends on how you’ve chosen to model this interaction. You can dispatch commands or use a service layer to trigger new actions.

Why are process managers useful?

When modelling large processes, process managers allow you break them up into multiple steps. This is especially useful when subsequent actions don’t require user interaction. All of these actions can be done in the background, thus not blocking the user from receiving a response from the server.

Process Manager Example

In this example I’m using tactician to dispatch new commands:

<?php

use EventSauce\EventSourcing\Consumer;
use EventSauce\EventSourcing\Message;
Use League\Tactician\CommandBus;

class ProductRestocker implements Consumer
{
    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }

    public function handle(Message $message)
    {
        $event = $message->event();
        
        if ($event instanceof ProductSoldOut) {
            $this->commandBus->handle(
                new RestockProduct($event->productId())
            );
        }
    }
}