5. Definition: Domain Event
• Martin Fowler: (PEAA, 2002)
▫ Captures the memory of something interesting
which affects the domain
▫ The essence […] is to capture things that can
trigger a change to the state
▫ These event objects are then processed to cause
changes to the system, and stored to provide an
audit log
17. An Event Stream …
Added Added Added Removed
to to to from
Cart Cart Cart Cart
18. An Event Stream …
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Shopping
Cart
Can be projected into:
19. An Event Stream …
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Sales History
Shopping
Cart
… or into this:
20. An Event Stream …
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Sales History
Audit Trail
Shopping
Cart
… or into this:
21. An Event Stream …
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Sales History
Campaign Effectiveness
Shopping
Campaign Effectiveness
Cart
… or into this:
23. Simple CQRS
Relational Data SQL
Storage Views
ORM Thin Read Layer
Domain Model
Command Handlers
Commands Queries
Client
24. Optimized Write / Complex to Read
INNER JOIN …
LEFT OUTER JOIN …
Relational Data UNION … SQL
Storage GROUP BY … Views
ORM Thin Read Layer
Domain Model
Command Handlers
Commands Queries
Client
25. Optimized Write / Complex to Read
INNER JOIN …
LEFT OUTER JOIN …
Relational Data UNION … SQL
Storage GROUP BY … Views
ORM Thin Read Layer
Domain Model
Command Handlers
Commands Queries
Client
26. Replace Views with Table per Query
SELECT * FROM …
Table
Relational Data per
Storage Query
ORM Thin Read Layer
Domain Model
Command Handlers
Commands Queries
Client
27. Emit Events when they occur
Table
Relational Data per
Storage View
ORM Thin Read Layer
Domain Model
Command Handlers
Commands Queries
Client
28. Denormalize Events into Read Model
Denormalizer
Table
Relational Data per
Storage View
ORM Thin Read Layer
Domain Model
Command Handlers
Commands Queries
Client
29. Add an Event Log for future retrieval
Denormalizer
Table
Relational Data per
Storage View
ORM Thin Read Layer
Domain Model
Command Handlers
Commands Event Log Queries
Client
30. Simplified:
Events
Domain Read Model
Commands DTOs
Client
41. Testing the Read Model
Denormalizer
Table
per
View
Thin Read Layer
42. Testing the Read Model
Given that certain Events have occurred
Denormalizer
Table
per
View
Thin Read Layer
43. Testing the Read Model
Given that certain Events have occurred
Denormalizer
Table
per
When a specific Event has occurred View
Thin Read Layer
44. Testing the Read Model
Given that certain Events have occurred
Denormalizer
Table
per
When a specific Event has occurred View
Thin Read Layer
Then each query should produce the expected results
45. Testing the Read Model
Given that a book with an ISBN of xxx and a
price of USD 24.00 was added to a new shopping cart
When a book with an ISBN of yyy and a price
of USD 19.00 was added to that same shopping cart
Then the Shopping Cart Summary should contain 2 books
and have a total amount of USD 43.00
And the Shopping Cart Details should contain book xxx
And the Shopping Cart Details should contain book yyy
47. Testing the Write Model
Given that certain Events have occurred
Domain Model
Command Handlers
48. Testing the Write Model
Given that certain Events have occurred
Domain Model
Command Handlers
When a specific Commands is sent
Commands
49. Testing the Write Model
Then the expected Events (and only those) should be emitted
Given that certain Events have occurred
Domain Model
Command Handlers
When a specific Commands is sent
Commands
50. Testing the Write Model
Given that an Add Book To Shopping Cart event with
an ISBN of xxx had been sent
When a Remove Book From Shopping Cart command is
sent with an ISBN of zzz
Then a Book Removal Failed event should be emitted
And NO Book Was Removed From Shopping Cart event
should be emitted
52. Main Value:
• Additive only, i.e. we don‘t lose information
• Linearly scalable and distributable Read Model
• Every new view can be created from the
beginning of time
• We can come up with new questions at any time
• Built-In Integration Model
53. Proven Technology:
Mature business models move away from
Update/Delete and become purely transactional:
▫ Human Resources ▫ Banking
▫ Medicine ▫ Financial Trading
▫ Bookkeeping ▫ Government
▫ …
54. What we haven‘t touched yet:
▫ Messaging ▫ Eventual Consistency
▫ Event Sourcing ▫ Integration Scenarios
▫ Sagas ▫…