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

Event Sourcing - History in the making

Event Sourcing - History in the making

Benjamin Dulau

February 19, 2015
Tweet

Other Decks in Programming

Transcript

  1. ❝ I want a list of all members that signed

    in at least four times between 1st January and 15th January 2014… and … I want to compare this to the same period range but in 2013. ❞ Business guy
  2. ID username last_login 1 ehamill 2007-01-22 03:04:39 2 lherman 2012-04-27

    10:38:28 3 darien13 2010-06-16 21:43:06 4 olind 2012-01-02 04:31:21 … … … Current data
  3. ID CartId SKU Qty 1 1 ABC 5 2 1

    DEF 1 3 1 IJK 10 Cart Cart lines ID CustomerId 1 1 2 1 3 1 1 0..*
  4. If we know the events of the past, we can

    reconstitute the present.
  5. A Domain Event is a Message emitted by the Domain

    Model to inform the system that something relevant happened.
  6. class CustomerWithdrewCash implements DomainEvent { public $customerId; public $accountId; public

    $amount; public $date; public function __construct($customerId, $accountId, $amount, $date) { $this->customerId = $customerId; $this->accountId = $accountId; $this->amount = $amount; $this->date = $date; } }
  7. { payload: { customerId: "123", accountId: "456", amount: 150, date:

    "2005-08-15T15:52:01+00:00" }, eventType: "CustomerWithdrewCash" }
  8. Reconstituting JSON JSON JSON JSON Fetch & unserialize Domain Event

    Domain Event Domain Event Domain Event Domain Model Aggregate apply
 one-by-one Event stream Domain events object collection
  9. class LatestMemberViewGenerator implements EventHandler { private $memberDAO; public function __construct(MemberDAO

    $memberDAO) { $this->memberDAO = $memberDAO; } public function whenMemberHasSignedUp(MemberHasSignedUp $event) { $this->memberDAO->addToLatestMemberList(new LatestMemberItem( $event->memberId, $event->name, $event->username, $event->email, $event->date )); } }
  10. Events in the domain should be explicitly expressed in the

    domain model and in the ubiquitous language.
  11. Being able to replay history is a major benefit both

    technically and for the business.