Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CQRS and Event Sourcing

CQRS and Event Sourcing

CQRS and Event Sourcing are becoming buzzwords nowadays. This presentation tries to clarify both concepts and present a simple example application, written with those powerful architectures

Christian

June 12, 2014
Tweet

More Decks by Christian

Other Decks in Programming

Transcript

  1. <?php ! namespace An\Awesome\Namespace; ! class AnAmazingService { /** @var

    An\Awesome\Dependency */ private $aDependency; ! public function getDependency() { if (null === $this->aDependency) { $this->aDependency = new \An\Awesome\Dependency(); } ! return $this->aDependency; } ! public function anIncredibleAction() { $aDependency = $this->getDependency(); $aDependency->performSomethingNeeded(); // ... } }
  2. <?php ! namespace An\Awesome\Namespace; ! class AnAmazingService { /** @var

    An\Awesome\Dependency */ private $aDependency; ! public function getDependency() { if (null === $this->aDependency) { $this->aDependency = new \An\Awesome\Dependency(); } ! return $this->aDependency; } ! public function anIncredibleAction() { $aDependency = $this->aDependency; $aDependency->performSomethingNeeded(); // ... } }
  3. <?php ! namespace AcmeShop\Billing\DomainModel\Order; ! interface OrderRepository { /** @return

    Order */ public function get(OrderId $anId); ! /** @return Order[] */ public function getByUser(UserId $aUserId); }
  4. <?php ! namespace AcmeShop\Billing\DomainModel\Order; ! interface OrderRepository { /** @return

    Order */ public function get(OrderId $anId); ! /** @return Order[] */ public function byUser(UserId $aUserId); ! /** @return OrderWithAge[] */ public function allGroupedByAge(); }
  5. <?php ! namespace AcmeShop\Billing\DomainModel\Order; ! interface OrderRepository { /** @return

    Order */ public function get(OrderId $anId); ! /** @return Order[] */ public function byUser(UserId $aUserId); ! /** @return OrderWithAge[] */ public function allGroupedByAge(); ! /** @return OrderWithCarrier[] */ public function allGroupedByCarrier(); }
  6. Command sends Command Handler is handled by Write Model uses

    Event Subscriber triggers writes reads from Read Model queries User Interface synchronizes with returns
  7. Command sends Command Handler is handled by Write Model uses

    Event Subscriber triggers writes reads from Read Model queries Eventual Consistency™ User Interface synchronizes with returns
  8. [ { "type":"PostWasCreated", "created_at": "2014-06-11 18:00:15", "data": "{'post_id': 1, 'title':

    '...', 'content': '...'}" }, { "type": "PostWasPublished", "created_at": "2014-06-11 18:05:35", "data": "{'post_id': 1}" } ]
  9. [ { "type":"PostWasCreated", "created_at": "2014-06-11 18:00:15", "data": "{'post_id': 1, 'title':

    '...', 'content': '...'}" }, { "type": "PostWasPublished", "created_at": "2014-06-11 18:05:35", "data": "{'post_id': 1}" }, { "type": "CommentWasCreated", "created_at": "2014-06-11 18:15:20", "data": "{'post_id': 1, 'comment_id': 1, 'comment': '...'}" } ]