cocoders / event-store
事件存储层,帮助我们(Cocoders)在项目中实现事件溯源
0.1.1
2016-03-10 16:01 UTC
Requires
- php: ^7.0
Requires (Dev)
- league/tactician: ^1.0
- phpspec/phpspec: ^2.4
- phpunit/phpunit: 5.2.*
- ramsey/uuid: ^3.1
This package is not auto-updated.
Last update: 2024-09-14 18:47:40 UTC
README
事件存储层,帮助我们 - Cocoders 在项目中实现事件溯源。这是一个相当简单(或者可以说是愚蠢)的抽象层(yunno KISS)
要求
PHP >= 7.0
安装
可以使用composer安装此包:composer require cocoders/event-store
使用
库提供了一系列接口和大约5个具体的类,用于处理领域事件。
您需要执行哪些步骤才能将此事件存储设置到您的项目中?遵循以下5个步骤。
-
步骤1 - 定义聚合。在您的聚合中,您需要实现
Cocoders\EventStore\AggregateRoot
接口(如果您想的话,可以使用Cocoders\EventStore\AggregateRootBehavior
特质)。例如:示例领域发票聚合 -
步骤2 - 定义自己的事件并在聚合中使用它。您应该定义新的事件类并在您的聚合中使用这些事件
-
步骤3 - 实现自己的EventStore。您需要创建
Cocoders\EventStore\EventStore
接口的具体实现,使用您最喜欢的数据库引擎;)。例如:Json File EventStore -
步骤4 - 对聚合进行一些操作,并将新鲜的事件从聚合添加到事件存储中,并提交事件存储。
示例
$eventStore = new MyEventStore(); $invoice = Invoice::issueInvoice( Invoice\Id::generate(); $command->getSeller(), $command->getBuyer(), $command->maxItemNumber ); $eventStore->apply(new EventStream\Name(Invoice::class), $invoice->getRecordedEvents()); $eventStore->commit();
- 步骤5 - 定义自己的投影。正如您在示例中看到的那样,发票聚合没有许多“获取器”。您应该使用投影而不是使用获取器来生成读取模型。投影基本上是事件订阅者,可以响应事件并更改读取模型。要使用事件订阅者,您需要使用事件总线
$eventStore = new MyEventStore(); $eventSubscribers = new EventSubscribers(); $eventBus = new EventBus($eventSubscribers); $projectionManager = new ProjectionManager($eventSubscribers); $projectionManager->registerProjection( 'InvoiceIssued', new MyProjection() ); $newEventsStream = $eventStore->findUncommited(new EventStream\Name(Invoice::class)); $eventStore->commit(); $eventStore->notify($newEventsStream); //now event bus will notify projections as well
- 更多示例:您可以在IntegrationWithExampleDomainTest中看到整个工作示例设置
开发和测试
为了开发这个库,我们使用docker和docker-compose。安装这些后,您应该运行
docker-compose up docker-compose run eventstore bash
然后在docker控制台中运行
composer install php bin/phpspec run -fpretty php bin/phpunit