command query responsibility segregation and event sourcing

22
Command Query Responsibility Segregation and Event Sourcing CQRS/ES Mitin Pavel cnc.dn.ua 2012

Upload: mitinpavel

Post on 07-Dec-2014

1.876 views

Category:

Technology


2 download

DESCRIPTION

Presentation for a coffee'n'code meeting in Donetsk

TRANSCRIPT

Page 1: Command Query Responsibility Segregation and Event Sourcing

Command Query Responsibility Segregation andEvent SourcingCQRS/ESMitin Pavel cnc.dn.ua 2012

Page 2: Command Query Responsibility Segregation and Event Sourcing

Authors

Greg YoungUdi Dahan

http://www.domainlanguage.com/

Page 3: Command Query Responsibility Segregation and Event Sourcing

Preface1. Probably you don't need CQRS/ES2. Do not consider CQRS to be a top-level

architectural pattern

Page 4: Command Query Responsibility Segregation and Event Sourcing

Driving forces1. Collaboration2. Staleness

Page 5: Command Query Responsibility Segregation and Event Sourcing

Query and command sides

http://www.gridshore.nl/2009/12/21/cqrs-made-easy-with-cqrs4j

Page 6: Command Query Responsibility Segregation and Event Sourcing

Event sourcing● The single source of truth● Capture all changes to an application state

as a sequence of events● Metaphor -- a version control system

Page 7: Command Query Responsibility Segregation and Event Sourcing

Event log facilities● Complete rebuild● Temporal query● Event replay

Page 8: Command Query Responsibility Segregation and Event Sourcing

Event Store● Simple event store consists of 78 lines of C#● Snapshots as performance optimization

Page 9: Command Query Responsibility Segregation and Event Sourcing

Persistence options● RDBMS● Aggregate-oriented database

Page 10: Command Query Responsibility Segregation and Event Sourcing

Validation● Validation ● Business rules

Page 11: Command Query Responsibility Segregation and Event Sourcing

Asynchronous interface● Validation results are available immediately● Business rule output are delayed in time

Page 12: Command Query Responsibility Segregation and Event Sourcing

Capture user's intentTask-based UI

Page 13: Command Query Responsibility Segregation and Event Sourcing

Code: commandhttps://github.com/gregoryyoung/m-r.git

public class DeactivateInventoryItem : Command { public readonly Guid InventoryItemId; public readonly int OriginalVersion;

public DeactivateInventoryItem(Guid inventoryItemId, int originalVersion) { InventoryItemId = inventoryItemId; OriginalVersion = originalVersion; }}

Page 14: Command Query Responsibility Segregation and Event Sourcing

Code: eventpublic class InventoryItemDeactivated : Event { public readonly Guid Id;

public InventoryItemDeactivated(Guid id) { Id = id; }}

Page 15: Command Query Responsibility Segregation and Event Sourcing

Code: controllerpublic class HomeController : Controller{ private FakeBus _bus; ...

public ActionResult Deactivate(Guid id, int version) { _bus.Send(new DeactivateInventoryItem(id, version)); return RedirectToAction("Index"); }

Page 16: Command Query Responsibility Segregation and Event Sourcing

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); }

Page 17: Command Query Responsibility Segregation and Event Sourcing

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; }

Page 18: Command Query Responsibility Segregation and Event Sourcing

Code: read modelpublic class InventoryListView : Handles<InventoryItemCreated>, Handles<InventoryItemRenamed>, Handles<InventoryItemDeactivated>{ ....

public void Handle(InventoryItemDeactivated message) { BullShitDatabase.list.RemoveAll(x => x.Id == message.Id); } }

Page 19: Command Query Responsibility Segregation and Event Sourcing

Code: controllerpublic class HomeController : Controller{ private ReadModelFacade _readmodel; ...

public ActionResult Details(Guid id) { ViewData.Model = _readmodel.GetInventoryItemDetails(id); return View(); }

Page 20: Command Query Responsibility Segregation and Event Sourcing

Polyglot programming● Javascript vs <you name it>● Static typing for the command side vs

dynamic typing for the query side● Core team vs outsourcers ;)

Page 21: Command Query Responsibility Segregation and Event Sourcing

Top level architecture● Don't do It. Just don't● CQRS frameworks push you toward the anti-

pattern

Page 22: Command Query Responsibility Segregation and Event Sourcing

Benefits of CQRS/ES● handling complexity● high-performance handling● integrating with third party systems● [ES] new reports from the historical data● [ES] behavioral analysis