thiagomarini/binocular
Composer 安装命令:
composer require thiagomarini/binocular
包简介
Doing Event Sourcing without building a spaceship
README 文档
README
Doing CQRS + Event Sourcing without building a spaceship. An attempt to bring event sourcing down from the over-engineering realm.
The aim of this project is to enable you to do CQRS + ES in PHP using your current stack, without the need to adopt any new technology. Sometimes classic database models are not enough to represent the state of an application. If you find yourself creating database views or using heavy SQL queries to present data in different ways Binocular is for you, it will bring structure and order to your application. A bit of a mindset shift is necessary to work with events tough, you'll have to think about producing and consuming events. But you don't need to do it everywhere or change the architecture of your application, you can do it only where database models are struggling to represent state.
This project focus only on 3 elements of ES + CQRS:
- Event stream: async stream of events produced by the application. The write side.
- Projections: will replay and process events from the event stream to calculate state.
- Read models: will cache the result of the event processing by the projection. The read side.
For more information please read my post supporting the idea.
What's different about it?
- Binocular is super lightweight and can be used with any framework.
- Should only be used where database models are struggling to represent state in the application.
- Projections use reducers to calculate the state of read models, a bit like Redux:
previousState + event = newState. Reducers make testing extremely simple, the same input always produces the same output. - Actions and reducers are versioned so events can evolve drama-free.
- The only premise is that events need to be persisted somewhere so they can be replayed.
- The project consists mostly of interfaces and base classes, you'll need to make your own implementation and know where to place things.
Why Binocular as project name?
Like CQRS, binocular vision happens when two separate images from two eyes are successfully combined into one image in the brain. CQRS has two eyes: the read and write eyes.
Usage in a nutshell
composer require thiagomarini/binocular
// save some events $eventRepository->store( new UserSignedUp($userId, ['name' => 'John']) ); $eventRepository->store( new UserNameWasUpdated($userId, ['name' => 'John Smith']) ); // use a projection to process the events and calculate the state of its read model $newState = $onboardingProjection->calculateState($userId); // save the read model state $readModelRepository->store($userId, $newState); print_r($newState); // ['name' => 'John Smith']
Laravel Example
As already explained Binocular can be used with any framework, you just need to know where to place things. In the case of Laravel, it already has a simple observer implementation which is more than enough to make things work with Binocular.
I've created an example app in Laravel. In the example I used the User model as the root to be event sourced, meaning that will have its own events table and also a read model table.
Conceptually you'll need to:
- Create an Eloquent implementation of the repository if you don't want to use the PDO one.
- Create migrations for event and read model tables.
- Create a custom implementation of the
event()global helper in order to save the event before queueing it. - Place a projection in an event listener to calculate and save the state of the read model.
- Fire events wherever you think it's appropriate.
- The cached state will be available on the read model and you can use it as any Eloquent model in the application. And remember that read models and projections are 1-1, meaning that one projection should produce state for one read model only.
There's also other plain PHP examples on tests/Examples folder.
How to contribute
PRs are welcome :)
thiagomarini/binocular 适用场景与选型建议
thiagomarini/binocular 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.04k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2019 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event」 「cqrs」 「sourcing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 thiagomarini/binocular 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thiagomarini/binocular 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 thiagomarini/binocular 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Interfaces, Traits and Nominal Classes used by Domain Entities Implementing Domain Events
Implementation of the transactional outbox pattern on top of rekalogika/domain-event
Event Sourcing implementation using prooph/event-store
Infrastructure and testing helpers for creating CQRS and event sourced applications.
Symfony bundle for broadway/broadway.
A Minimalistic PHP Event Sourcing / CQRS Library with GDPR Support
统计信息
- 总下载量: 3.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-20