wwwision/dcb-example
Composer 安装命令:
composer create-project wwwision/dcb-example
包简介
Simple example for the Dynamic Consistency Boundary pattern described by Sara Pellegrini
README 文档
README
Simple example for the Dynamic Consistency Boundary pattern described by Sara Pellegrini.
The purpose of this package is to explore the idea, find potential pitfalls and to spread the word.
tl;dr Have a look at the example script or Behat Tests to see this in action.
Background
Dynamic Consistency Boundary (aka DCB) allow to enforce hard constraints in Event-Sourced systems without having to rely on individual Event Streams. This facilitates focussing on the behavior of the Domain Model rather than on its rigid structure. It also allows for simpler architecture and potential performance improvements as multiple projections can act on the same events without requiring synchronization.
Read all about this interesting approach in the blog post mentioned above or watch Saras talk on YouTube (Italian with English subtitles). This package models the example of this presentation (with a few deviations) using the wwwision/dcb-eventstore package and the wwwision/dcb-eventstore-doctrine database adapter.
Important Classes / Concepts
- Commands are just a concept of this example package. They implement the Command Marker Interface
- The CommandHandler is the central authority, handling and verifying incoming Command
- It uses in-memory Projections to enforce hard constraints
- For each command handler a DecisionModel instance is built that contains the state of those in-memory projections and the AppendCondition for new events
- The EventSerializer can convert DomainEvent instances to writable events, vice versa
- Note: This package contains no Read Model (i.e. classic projections) yet
Considerations / Findings
I always had the feeling, that the focus on Event Streams is a distraction to Domain-driven design. So I was very happy to come across this concept. In the meantime I have had the chance to test it in multiple real world scenarios, and it works really well for me and simplifies things (in spite of some minor caveats in the current implementation):
- It becomes trivial to enforce constraints involving multiple entities (like in this example).
- Global uniqueness (aka "the unique username problem") can easily be achieved with DCB
- Consecutive sequences (e.g. invoice number) can be done without reservation patterns and by only reading a single event per constraint check
- When using composition like in this example, phe in-memory projections are surprisingly small because they focus on a single responsibility
- ...and more
Usage
Install via composer:
composer create-project wwwision/dcb-example-courses
Now you should be able to run the example script via
php dcb-example-courses/index.php
And you should get ...no output at all. That's because the example script currently satisfy all constraints. Try changing the script to test, that the business rules are actually enforced, for example you could add the line:
$commandHandler->handle(SubscribeStudentToCourse::create(courseId: 'c1', studentId: 's2'));
to the end of the file, which should lead to the following exception:
Failed to subscribe student with id "s2" to course with id "c1" because a student with that id does not exist
Alternatively, you could have a look at the Behat Tests:
Tests
This package comes with 16 Behat scenarios covering all business features. You can run the tests via
composer test:behat
Acknowledgment
Most of the implementation of these packages are based on the great groundwork done by Sara Pellegrini, so all praise belong to her!
Contributions
I'm really curious to get feedback on this one. Feel free to start/join a discussion, issues or Pull requests.
wwwision/dcb-example 适用场景与选型建议
wwwision/dcb-example 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 21, 最近一次更新时间为 2023 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wwwision/dcb-example 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wwwision/dcb-example 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-05