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

Future is a Thing of the Past

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Future is a Thing of the Past

Traditional approaches to data persistence record results rather than intentions. Event Sourcing provides us a way to do the exact opposite. In this talk, we explore the freedoms gained by throwing off the shackles of state persistence.

Avatar for Mitchell van Wijngaarden

Mitchell van Wijngaarden

August 23, 2016

Other Decks in Programming

Transcript

  1. DDD

  2. DDD

  3. class BoardGameStockWasReplenished implements DomainEvent { private $boardGameId; private $amount; function

    __constructor(BoardGameId $boardGameId, $amount) { $this->boardGameId = $boardGameId; $this->amount = $amount; } // getters... }
  4. Colt Express Quantum Condottiere BoardGameStockWas Replenished StartedSellingBoardGame BoardGameManufacturing Stopped StartedSellingBoardGame

    StartedSellingBoardGame BoardGameStockWas Replenished BoardGameStockWas Replenished BoardGameStockWas Replenished
  5. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  6. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  7. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  8. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  9. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  10. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  11. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  12. public static function startSelling($name, $price) { $id = Uuid::v4(); $boardGame

    = new BoardGame; $boardGame->record(new StartedSellingBoardGame($id, $name, $price)); $boardGame->applyStartedSellingBoardGame($event); return $boardGame; }
  13. public static function rebuildFrom(Events $events) { $object = new BoardGame;

    /** @var Event $event */ foreach ($events as $event) $object->applyEvent($event); return $object; }
  14. protected function applyEvent(Event $event) { if ($event instanceof StartedSellingBoardGame) $this->applyStartedSellingBoardGame($event);

    elseif ($event instanceof BoardGameStockWasReplenished) $this->applyBoardGameStockWasReplenished($event); // more stuff }
  15. projection # name price stock discount 1 Machi Koro €21

    14 0 2 Exploding Kittens €15 0 0 ... ... ... ... ... 13 Quantum €50 3 10%
  16. trial ends at 10 invoices sent invoices 4 InvoiceW asSent

    InvoiceW asSent InvoiceW asSent InvoiceW asSent
  17. trial ends at 10 invoices sent invoices 8 InvoiceW asSent

    InvoiceW asSent InvoiceW asSent InvoiceW asSent ...
  18. trial ends at 10 invoices sent invoices 9 InvoiceW asSent

    InvoiceW asSent InvoiceW asSent InvoiceW asSent ... InvoiceW asSent
  19. trial ends at 10 invoices InvoiceW asSent InvoiceW asSent InvoiceW

    asSent InvoiceW asSent ... InvoiceW asSent InvoiceW asSent sent invoices 10