dsantang/domain-events-doctrine
Composer 安装命令:
composer require dsantang/domain-events-doctrine
包简介
Package that provides an integration with Doctrine ORM to automatically dispatch Domain Events.
README 文档
README
This package is meant to be used in conjunction with dsantang/domain-events,
which provides the basic building blocks needed in order to create your application's domain events.
This package provides an integration with Doctrine ORM,
in order to automatically dispatch the recorded domain events once the ORM's flush() operation is successful.
Events are dispatched via a Symfony's EventDispatcherInterface.
Installation
The suggested installation method is via composer:
php composer.phar require dsantang/domain-events-doctrine
Configuration
In order to automagically dispatch your domain events, you first need to add two event listeners to your Doctrine's EventManager.
These Doctrine's listeners will be listening to Doctrine's lifecycle events, and, only once the transaction has successfully been made,
they'll dispatch your domain events.
use Dsantang\DomainEventsDoctrine\EventsRecorder\DoctrineEventsRecorder; use Dsantang\DomainEventsDoctrine\Dispatcher\DoctrineEventsDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher; use Doctrine\Common\EventManager; // ... $evm = new EventManager(); $dispatcher = new EventDispatcher(); // You can register here your domain event listeners to your dispatcher... $tracker = new EventsTracker(); // Make sure they share the same `EventsTracker` instance. $evm->addEventListener([Events::onFlush], new DoctrineEventsRecorder($tracker)); $evm->addEventListener([Events::postFlush], new DoctrineEventsDispatcher($tracker, $dispatcher)); // Remember to correctly wire this EventManager to your ORM.
That's it! You're all set!
Now you can add as many Symfony's listeners as you need to your $dispatcher,
and you'll be able to react to the domain events raised by your application.
Outbox pattern
This library also provides support for the Outbox pattern implementation.
The idea behind the implementation is to be able to add entities to an "ongoing" transaction by hooking into Doctrine's onFlush event,
creating "outbox" entries based on the application's domain events, and safely store them using the same DB transaction.
In order to enrich your Domain Event and be able to set all the data required by the Outbox entity,
you need to create a Converter class (by implementing Dsantang\DomainEventsDoctrine\Outbox\Converter interface)
An example of an outbox event is as follows:
use Dsantang\DomainEvents\DomainEvent; use Dsantang\DomainEventsDoctrine\Outbox\Converter; use Dsantang\DomainEventsDoctrine\Outbox\OutboxEntry; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; final class YourOutboxConverter implements Converter { public function convert(DomainEvent $domainEvent) : OutboxEntry { return new YourOutboxEntry($domainEvent); } } final class YourOutboxEntry implements OutboxEntry { /** @var DomainEvent */ private $domainEvent; public function __construct(DomainEvent $domainEvent) { $this->domainEvent = $domainEvent; } public function getName() : string { return 'OrderDispatched'; } public function getAggregateId() : UuidInterface { return Uuid::fromString('d1702762-548b-11e9-8647-d663bd873d93'); } public function getAggregateType() : string { return 'Order'; } public function getPayloadType() : string { return 'OrderStructure'; } public function getMessageKey() : string { return 'd663bd873d93'; } public function getMessageRoute() : string { return 'aggregate.order'; } public function getMessageType() : string { return 'OrderCreated'; } public function getPayload() : string { return json_encode($this->domainEvent) } public function getSchemaVersion() : int { return 1; } }
In order to persist your Outbox entries, you must create a Doctrine Entity class inside your application that extends
Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass.
Please note that this approach uses Doctrine's Inheritance Mapping:
Here is an example of an Outbox Entity class:
namespace YourNamespace; use Doctrine\ORM\Mapping as ORM; use Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass; /** * @ORM\Entity() * @ORM\Table */ class YourOutboxEntity extends OutboxMappedSuperclass { /** * @ORM\Column(type="string") * * @var string */ private $someAdditionalField; }
And an example of the required configuration as is follows:
Warning: this solution assumes that you're using Dsantang\DomainEvents\DomainEvent in order to raise your domain events.
use Dsantang\DomainEventsDoctrine\Outbox\MapBased; use Dsantang\DomainEventsDoctrine\Outbox\OutboxMappedSuperclass; // Your class must extend OutboxMappedSuperclass $yourOutboxEntity = new YourOutboxEntity(); $mapBased = new MapBased($yourOutboxEntity); $mapBased->addConverter('YouNamespace\YourDomainEvent', new YourOutboxConverter()); // Always use with OnFlush event $evm->addEventListener([Events::onFlush], $mapBased);
dsantang/domain-events-doctrine 适用场景与选型建议
dsantang/domain-events-doctrine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.12k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「doctrine」 「dispatcher」 「Domain Driven Design」 「domain event」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dsantang/domain-events-doctrine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dsantang/domain-events-doctrine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dsantang/domain-events-doctrine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
Event dispatcher package
Make pimcore migration simple
PeskyORM - annoying ORM that contains tons of exceptions and throws them when anything goes wrong
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
统计信息
- 总下载量: 24.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-09