承接 dsantang/domain-events-doctrine 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

CircleCI Scrutinizer Code Quality Code Coverage Build Status SymfonyInsight

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 dsantang/domain-events-doctrine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-09