Links

Persistence Strategy

PHP Event Sourcing Persistence Strategy

Persistence Strategy

Describes how streams with events will be stored.

Single Stream Strategy

The default persistence strategy is Single Stream Strategy. This persistence stores all instances of specific aggregate, within same stream.
#[ServiceContext]
public function persistenceStrategy()
{
return \Ecotone\EventSourcing\EventSourcingConfiguration::createWithDefaults()
->withSingleStreamPersistenceStrategy();
}
namespace Domain\Ticket;
#[EventSourcingAggregate]
class Ticket
All instances of Ticket will be stored within Domain\Ticket\Ticket stream.

Stream Per Aggregate Strategy

This persistence creates stream per aggregate instance.
#[ServiceContext]
public function persistenceStrategy()
{
return \Ecotone\EventSourcing\EventSourcingConfiguration::createWithDefaults()
->withStreamPerAggregatePersistenceStrategy();
}
namespace Domain\Ticket;
#[EventSourcingAggregate]
class Ticket
Instances of Ticket will be stored within Domain\Ticket\Ticket-{ticketId} stream where ticketId is identifier of specific aggregate.

Custom Strategy

You may provide your own Customer Persistence Strategy as long as it implements PersistenceStrategy.
#[ServiceContext]
public function aggregateStreamStrategy()
{
return EventSourcingConfiguration::createWithDefaults()
->withCustomPersistenceStrategy(new CustomStreamStrategy(new FromProophMessageToArrayConverter()));
}

Custom Stream Name

If you want to make use of custom stream name (default is Aggregate class name), then you can apply Stream attribute to your aggregate.
#[Stream("basket_stream")]
class Basket
Then tell the projection to make use of it:
#[Projection(self::PROJECTION_NAME, "basket_stream")]
class BasketList

Custom Aggregate Type

By default events in the stream will hold Aggregate Class as AggregateType. You may customize this by applying AggregateType attribute to your Aggregate.
#[AggregateType("basket")]
class Basket
Setting this up will allow you to refactor the aggregate class name or namespace and still load the same Aggregate.