andreo/eventsauce-snapshotting

为EventSauce扩展的快照组件。

3.4 2023-03-28 18:05 UTC

This package is auto-updated.

Last update: 2024-09-28 21:28:49 UTC


README

为EventSauce扩展的快照组件

关于快照

安装

composer require andreo/eventsauce-snapshotting

先前版本

要求

  • PHP >=8.2
  • Doctrine Dbal ^3.1

Doctrine 快照存储库

use Andreo\EventSauce\Snapshotting\Doctrine\DoctrineSnapshotRepository;

new DoctrineSnapshotRepository(
    connection: $connection, // Doctrine\DBAL\Connection
    tableName: $tableName,
    serializer: $serializer, // Andreo\EventSauce\Snapshotting\Serializer\SnapshotStateSerializer
    uuidEncoder: $uuidEncoder, // EventSauce\UuidEncoding\UuidEncoder
    tableSchema: $tableSchema // Andreo\EventSauce\Snapshotting\Repository\Table\SnapshotTableSchema
)

版本控制

存储库

use Andreo\EventSauce\Snapshotting\Versioned\AggregateRootRepositoryWithVersionedSnapshotting;

new AggregateRootRepositoryWithVersionedSnapshotting(
    aggregateRootClassName: $aggregateRootClassName,
    messageRepository: $messageRepository
    regularRepository: $regularRepository, // EventSauce\EventSourcing\AggregateRootRepository
    snapshotVersionInflector: $snapshotVersionInflector, // Andreo\EventSauce\Snapshotting\Repository\Versioned\SnapshotVersionInflector
    snapshotVersionComparator: $snapshotVersionComparator // Andreo\EventSauce\Snapshotting\Repository\Versioned\SnapshotVersionComparator
);

版本化快照状态

use Andreo\EventSauce\Snapshotting\Versioned\VersionedSnapshotState;

final class FooSnapshotStateV2 implements VersionedSnapshotState {

    public static function getSnapshotVersion(): int|string|Stringable
    {
        return 2;
    }
}

聚合示例

use Andreo\EventSauce\Snapshotting\Aggregate\VersionedSnapshottingBehaviour;
use EventSauce\EventSourcing\AggregateRootBehaviour;
use EventSauce\EventSourcing\AggregateRootId;
use EventSauce\EventSourcing\Snapshotting\AggregateRootWithSnapshotting;

final class FooAggregate implements AggregateRootWithSnapshotting
{
    use AggregateRootBehaviour;
    use VersionedSnapshottingBehaviour;

    // Create snapshot method must have type hint of VersionedSnapshotState implementation (with default SnapshotVersionInflector)
    protected function createSnapshotState(): FooSnapshotStateV2
    {
        return new FooSnapshotStateV2();
    }

    protected static function reconstituteFromSnapshotState(AggregateRootId $id, $state): AggregateRootWithSnapshotting
    {
        assert($state instanceof FooSnapshotStateV2);
    }
}

条件策略

interface ConditionalSnapshotStrategy
{
    public function canStoreSnapshot(AggregateRootWithSnapshotting $aggregateRoot): bool;
}

内置策略

每n个事件

use Andreo\EventSauce\Snapshotting\Conditional\AggregateRootRepositoryWithConditionalSnapshot;use Andreo\EventSauce\Snapshotting\Conditional\EveryNEventConditionalSnapshotStrategy;

new AggregateRootRepositoryWithConditionalSnapshot(
    regularRepository: $regularRepository, // EventSauce\EventSourcing\Snapshotting\AggregateRootRepositoryWithSnapshotting
    conditionalSnapshotStrategy: new EveryNEventConditionalSnapshotStrategy(numberOfEvents: 200) # or your implementation
);