1 / 27

Event Sourcing

Event Sourcing . Cameron Fletcher. Agenda. What is an Event? Events as a Storage Mechanism Versioning Testing with Events Performance and Scalability Demonstration. What is an Event?. An Event.

earl
Télécharger la présentation

Event Sourcing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event Sourcing Cameron Fletcher

  2. Agenda • What is an Event? • Events as a Storage Mechanism • Versioning • Testing with Events • Performance and Scalability • Demonstration

  3. What is an Event?

  4. An Event An event is something that has happened in the past as a result of a change in state of our domain Glass has been filled Fill glass Event Request to change state Glass is already full! Glass is broken! Exception

  5. Events as aStorage Mechanism

  6. Shopping Cart Created Item Added Carts CartId HasCheckedOut Item Added True 1 False Item Added CartItems Name CartId ItemId Checkout Completed Domain Driven Design 1 1 Enterprise Integration Patterns 1 2 Event Centric 1 3

  7. Shopping Cart Created Item Added Item Added Item Added Domain Object Checkout Completed

  8. Shopping Cart Created Item Added Item Added Item Added Item Removed Checkout Completed

  9. Versioning

  10. 7 6 5 snapshot 4 3 2 1

  11. Cart Domain Object Example // request to mutate state publicvoidAdd(Item item) { if (this.itemCount > 5) thrownewCartFullException(this.id); ApplyChange( newItemAdded(this.id, item.Name)); } // mutate state privatevoidApply(ItemAdded @event) { this.itemCount++; }

  12. Domain Object Base protectedvoid ApplyChange(Event @event) { this.ApplyChange(@event, true); } privatevoid ApplyChange(Event@event, bool isNew) { this.AsDynamic().Apply(@event); if (isNew) this.uncommittedChanges.Add(@event); }

  13. Domain Object Reconstitution publicvoidLoadFromHistory(IEnumerable<Event> history) { foreach (var @event in history) this.ApplyChange(@event, false); }

  14. Domain Object Persistence publicIEnumerable<Event> GetUncommittedChanges() { returnthis.uncommittedChanges; } publicvoid MarkChangesAsCommitted() { this.uncommittedChanges.Clear(); }

  15. Testing with Events

  16. Overdraw Attempts are Rejected Given An account with a balance of 100 When A debit is requested for 101 Then An InsufficientBalanceException is thrown

  17. Overdraw Attempts are Rejected Given An account is created A deposit was made for 100 When A debit is requested for 101 Then An InsufficientBalanceException is thrown

  18. Overdraw Attempts are Rejected Given A series of events When A command Then Zero or more events, or an exception is thrown

  19. Overdraw Attempts are Rejected Given An account is created A deposit was made for 100 A debit is requested for 101 When A debit is requested for 20 Then An InsufficientBalanceException is thrown

  20. Performance and Scalability

  21. Performance and Scalability Benefits • Append-only model • Partitioning (Sharding) • Persisting Objects • Reconstituting Objects

  22. Demonstration

  23. Summary

  24. Questions?

More Related