Installation and First Steps
composer require ecotone/pdo-event-sourcing
Ecotone Event Sourcing is based on well known and stable Prooph's Event Store.
It does provide support for three databases:
- PostgreSQL
- MySQL
- MariaDB
There are two ways, we can make use of Event Sourced Aggregates.
This way of handling events does allow for pure functions. Writes are clearly separated from writes.
#[EventSourcingAggregate] // 1
class Ticket
{
use WithAggregateVersioning; // 2
#[AggregateIdentifier] // 1
private string $ticketId;
private string $ticketType;
#[CommandHandler] // 2
public static function register(RegisterTicket $command) : array
{
return [new TicketWasRegistered($command->getTicketId(), $command->getTicketType())];
}
#[CommandHandler] // 2
public function close(CloseTicket $command) : array
{
return [new TicketWasClosed($this->ticketId)];
}
#[EventSourcingHandler] // 4
public function applyTicketWasRegistered(TicketWasRegistered $event) : void
{
$this->ticketId = $event->getTicketId();
$this->ticketType = $event->getTicketType();
}
}
- 1.
- 2.Event Sourced Aggregate must provide version. You may leave it to
Ecotone
usingWithAggregateVersioning
or you can implement it yourself. - 3.
CommandHandler
for event sourcing returns events generated by specific method. This will be passed to theRepository
to be stored. - 4.
EventSourcingHandler
is method responsible for reconstructingAggregate
from previously created events. At least one event need to be handled in order to provideAggregateIdentifier
.
#[EventSourcingAggregate(true)] // 1
class Basket
{
use WithAggregateEvents;
use WithAggregateVersioning;
#[AggregateIdentifier]
private string $id;
#[CommandHandler] // 2
public static function create(CreateBasket $command) : static
{
$basket = new static();
$basket->recordThat(new BasketWasCreated($command->getId()));
return $basket;
}
#[CommandHandler] // 2
public function addProduct(AddProduct $command) : void
{
$this->recordThat(new ProductWasAddedToBasket($this->id, $command->getProductName()));
}
#[EventSourcingHandler]
public function applyBasketWasCreated(BasketWasCreated $basketWasCreated)
{
$this->id = $basketWasCreated->getId();
}
}
- 1.In order to make use of alternative way of handling events, we need to set attribute to true
EventSourcingAggregate(true)
- 2.Command Handlers instead of returning events are acting the same as State Stored Aggregates. All events which will be published using
recordThat
will be passed to theRepository
to be stored.
Last modified 11mo ago