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 the Single Stream Strategy. This persistence stores all instances of specific aggregate, within the same stream.

#[ServiceContext]
public function persistenceStrategy(): EventSourcingConfiguration
{
    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 a 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 an identifier of a 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()));
}

Multiple Persistence Strategies

Once set, the persistence strategy will apply to all streams in your application. However, you may face a situation when you need to have a different strategy for one or more of your streams.

#[ServiceContext]
public function eventSourcingConfiguration(): EventSourcingConfiguration
{
    return EventSourcingConfiguration::createWithDefaults()
        ->withSimpleStreamPersistenceStrategy()
        ->withPersistenceStrategyFor('some_stream', LazyProophEventStore::AGGREGATE_STREAM_PERSISTENCE)
    ;
}

The above will make the Simple Stream Strategy as default however, for some_stream Event Store will use the Aggregate Stream Strategy.

Please, be aware that you won't be able to set Custom Strategy that way.

Custom Stream Name

If you want to make use of a custom stream name (the 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 name 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.

Last updated