CommandQueryResponsibilitySegregation andEvent SourcingCQRS/ESMitin Pavelcnc.dn.ua 2012
Authors          Greg Young           Udi Dahan            http://www.domainlanguage.com/
Preface1. Probably you dont need CQRS/ES2. Do not consider CQRS to be a top-level   architectural pattern
Driving forces1. Collaboration2. Staleness
Query and command sideshttp://www.gridshore.nl/2009/12/21/cqrs-made-easy-with-cqrs4j
Event sourcing● The single source of truth● Capture all changes to an application state  as a sequence of events● Metaphor...
Event log facilities● Complete rebuild● Temporal query● Event replay
Event Store● Simple event store consists of 78 lines of C#● Snapshots as performance optimization
Persistence options● RDBMS● Aggregate-oriented database
Validation● Validation● Business rules
Asynchronous interface● Validation results are available immediately● Business rule output are delayed in time
Capture users intentTask-based UI
Code: commandhttps://github.com/gregoryyoung/m-r.gitpublic class DeactivateInventoryItem : Command {  public readonly Guid...
Code: eventpublic class InventoryItemDeactivated : Event {  public readonly Guid Id;    public InventoryItemDeactivated(Gu...
Code: controllerpublic class HomeController : Controller{  private FakeBus _bus;  ...  public ActionResult Deactivate(Guid...
Code: command handlerpublic class InventoryCommandHandlers{  private readonly IRepository<InventoryItem> _repository;  ......
Code: write modelpublic class InventoryItem : AggregateRoot{  public void Deactivate()  {     if(!_activated)         thro...
Code: read modelpublic class InventoryListView : Handles<InventoryItemCreated>,  Handles<InventoryItemRenamed>, Handles<In...
Code: controllerpublic class HomeController : Controller{  private ReadModelFacade _readmodel;  ...  public ActionResult D...
Polyglot programming● Javascript vs <you name it>● Static typing for the command side vs  dynamic typing for the query sid...
Top level architecture● Dont do It. Just dont● CQRS frameworks push you toward the anti-  pattern
Benefits of CQRS/ES●   handling complexity●   high-performance handling●   integrating with third party systems●   [ES] ne...
Upcoming SlideShare
Loading in …5
×

Command Query Responsibility Segregation and Event Sourcing

1,967 views

Published on

Presentation for a coffee'n'code meeting in Donetsk

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,967
On SlideShare
0
From Embeds
0
Number of Embeds
749
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Command Query Responsibility Segregation and Event Sourcing

  1. 1. CommandQueryResponsibilitySegregation andEvent SourcingCQRS/ESMitin Pavelcnc.dn.ua 2012
  2. 2. Authors Greg Young Udi Dahan http://www.domainlanguage.com/
  3. 3. Preface1. Probably you dont need CQRS/ES2. Do not consider CQRS to be a top-level architectural pattern
  4. 4. Driving forces1. Collaboration2. Staleness
  5. 5. Query and command sideshttp://www.gridshore.nl/2009/12/21/cqrs-made-easy-with-cqrs4j
  6. 6. Event sourcing● The single source of truth● Capture all changes to an application state as a sequence of events● Metaphor -- a version control system
  7. 7. Event log facilities● Complete rebuild● Temporal query● Event replay
  8. 8. Event Store● Simple event store consists of 78 lines of C#● Snapshots as performance optimization
  9. 9. Persistence options● RDBMS● Aggregate-oriented database
  10. 10. Validation● Validation● Business rules
  11. 11. Asynchronous interface● Validation results are available immediately● Business rule output are delayed in time
  12. 12. Capture users intentTask-based UI
  13. 13. Code: commandhttps://github.com/gregoryyoung/m-r.gitpublic class DeactivateInventoryItem : Command { public readonly Guid InventoryItemId; public readonly int OriginalVersion; public DeactivateInventoryItem(Guid inventoryItemId, int originalVersion) { InventoryItemId = inventoryItemId; OriginalVersion = originalVersion; }}
  14. 14. Code: eventpublic class InventoryItemDeactivated : Event { public readonly Guid Id; public InventoryItemDeactivated(Guid id) { Id = id; }}
  15. 15. Code: controllerpublic class HomeController : Controller{ private FakeBus _bus; ... public ActionResult Deactivate(Guid id, int version) { _bus.Send(new DeactivateInventoryItem(id, version)); return RedirectToAction("Index"); }
  16. 16. Code: command handlerpublic class InventoryCommandHandlers{ private readonly IRepository<InventoryItem> _repository; ... public void Handle(DeactivateInventoryItem message) { // Construct item from stream of events var item = _repository.GetById(message.InventoryItemId); item.Deactivate(); _repository.Save(item, message.OriginalVersion); }
  17. 17. Code: write modelpublic class InventoryItem : AggregateRoot{ public void Deactivate() { if(!_activated) throw new InvalidOperationException("already deactivated"); ApplyChange(new InventoryItemDeactivated(_id)); } private void Apply(InventoryItemDeactivated e) { _activated = false; }
  18. 18. Code: read modelpublic class InventoryListView : Handles<InventoryItemCreated>, Handles<InventoryItemRenamed>, Handles<InventoryItemDeactivated>{ .... public void Handle(InventoryItemDeactivated message) { BullShitDatabase.list.RemoveAll(x => x.Id == message.Id); }}
  19. 19. Code: controllerpublic class HomeController : Controller{ private ReadModelFacade _readmodel; ... public ActionResult Details(Guid id) { ViewData.Model = _readmodel.GetInventoryItemDetails(id); return View(); }
  20. 20. Polyglot programming● Javascript vs <you name it>● Static typing for the command side vs dynamic typing for the query side● Core team vs outsourcers ;)
  21. 21. Top level architecture● Dont do It. Just dont● CQRS frameworks push you toward the anti- pattern
  22. 22. Benefits of CQRS/ES● handling complexity● high-performance handling● integrating with third party systems● [ES] new reports from the historical data● [ES] behavioral analysis

×