wwwision / dcb-example
Simple example for the Dynamic Consistency Boundary pattern described by Sara Pellegrini
Fund package maintenance!
bwaidelich
Paypal
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 0
Open Issues: 3
Type:project
Requires
- php: >=8.2
- ramsey/uuid: ^4.7
- webmozart/assert: ^1.11
- wwwision/dcb-eventstore: ^1.0
- wwwision/dcb-eventstore-doctrine: ^1.0
Requires (Dev)
- behat/behat: ^3.13
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.1
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^4.0.x-dev
This package is auto-updated.
Last update: 2024-10-30 02:10:57 UTC
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 Commands
- ...it uses in-memory Projections to enforce hard constraints
- The Projections are surprisingly small because they focus on a single responsibility (e.g. instead of a "CourseAggregate" there are three projections CourseExistenceProjection.php, CourseTitleProjection and CourseCapacityProjection.php)
- The EventAppender allows for easy publishing of Events that are serialized with the EventNormalizer
- 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. So far I didn't have the chance to test it in a real world scenario, but it makes a lot of sense to me and IMO this example shows, that the approach really works out in practice (in spite of some minor caveats in the current implementation).
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(new SubscribeStudentToCourse(CourseId::fromString('c1'), StudentId::fromString('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.