Search by

gember / event-sourcing

jerowork

Use case driven EventSourcing - Let go of the Aggregate with the Dynamic Consistency Boundary (DCB) pattern.

Package info

github.com/GemberPHP/event-sourcing

pkg:composer/gember/event-sourcing

Statistics

Installs: 2 948

Dependents: 3

Suggesters: 0

Stars: 8

Open Issues: 0


README

Build Status Software License PHP Version

Use case driven EventSourcing - Let go of the Aggregate with the Dynamic Consistency Boundary (DCB) pattern.

Documentation

Full documentation is available at gemberphp.github.io/event-sourcing.

Upgrading? See UPGRADE.md.

In a nutshell

Traditional 'Aggregate driven' EventSourcing

Domain concepts are modeled towards objects: the aggregate.

  • Any business logic related to a single domain object should live inside the aggregate
  • Logic that involves other domain objects or groups of the same kind of domain objects does not belong in the aggregate

aggregate-driven-event-sourcing

'Use case driven' EventSourcing

Domain concepts are modeled through use cases.

  • Any business logic tied to a use case should live inside that use case
  • A use case can relate to one or more domain concepts

use-case-driven-event-sourcing

A simple example

A use case that subscribes a student to a course, protecting its rule against both the course's and the student's events:

final class SubscribeStudentToCourse implements EventSourcedUseCase
{
    use EventSourcedUseCaseBehaviorTrait;

    #[DomainTag]
    private CourseId $courseId;

    #[DomainTag]
    private StudentId $studentId;

    private bool $isSubscribed = false;

    #[DomainCommandHandler]
    public function __invoke(SubscribeStudentCommand $command): void
    {
        if ($this->isSubscribed) {
            return;
        }

        $this->apply(new StudentSubscribedEvent((string) $this->courseId, (string) $this->studentId));
    }

    // #[DomainEventSubscriber] methods rebuild the state from past events
}

See the full example with domain events and a saga, Getting started, and the demo application gember/example-event-sourcing-dcb.