1. From DDD to CQRS
presented by Dennis Traub
.NET Usergroup Bern - 19. MΓ€rz 2013
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
2. Whoβs that guy?
Dennis Traub
Founder, Owner, CEO, Single Employee of
D. Traub Software Development Consultancy
Consultant, developer, trainer, speaker
39 years old
Married, father of one
20+ years in software development
Hire me at mail@dennistraub.de
@dtraub
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
3. AGENDA
DDD Recap CQRS Break Step by Step
the strategic patterns probably simpler than you think! towards CQRS
1 2 3 4 5 6
βBest Practiceβ Domain Events End
what we call an Architecture are first class citizens Questions & Answers
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
4. AGENDA
DDD Recap CQRS Step by Step
the strategic patterns probably simpler than you think! towards CQRS
1 2 3 4 5 6
βBest Practiceβ Domain Events End
what we call an Architecture are first class citizens Questions & Answers
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
5. The βBlue Bookβ by Eric Evans
Image Placeholder
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
6. Domain-Driven Design
Domain
Service
Value Object
Aggregate
Specification The well-
known
Reposit ory Patterns Entity
Factory And so on . .
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
7. Domain-Driven Design
Ubiquit ous Language
Core Domain
Domain Expert
Bounded Context The
important
Domain Model Patterns (Generic) Subdomains
Context Map And so on . .
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
8. donβt build one big model
that works for everyone
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
9. the tactical Building Blocks
we donβt use DDD
when there is no value in
formalizing the problem
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
10. we only use DDD in parts
where we get a competitive
advantage
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
11. we focus our effort and resources on
the most important subdomain
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
12. a.k.a. The Core Domain
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
13. AGENDA
DDD Recap CQRS Step by Step
the strategic patterns probably simpler than you think! towards CQRS
1 2 3 4 5 6
βBest Practiceβ Domain Events End
what we call an Architecture are first class citizens Questions & Answers
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
14. Presentation Layer
Application Services
Domain / Business Model / BLL
DAL / O/R-Mapper
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
15. Pros of the βBest Practice Architectureβ
1 Easy to build
2 Accepted by management
3 Well-know throughall all seniority levels
4 We know ist limitiations
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
16. Cons of the βBest Practice Architectureβ
1 Limited scalability
2 Loss of intent
3 Lazy loading, locking, race conditions, β¦
4 Few classes do way too much stuff
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
17. And:
If the model is basically
CRUD
where does Business Logic live?
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
18. Domain Model Responsibilities
1 Business Logic 6 Aggregation
2 Validation 7 Presentation
3 Structure 8 Persistence
4 Projection 9 Processes
5 Associations 10 Lazy / Eager Loading
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
19. the S in SOLID
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
20. Single Responsibility Principle
just because we can doesnβt mean we should
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
21. why do we try to build
One Model to Rule Them All?
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
22. reading and writing are
completely different from eachother
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
23. Sometimes itβs cheaper to do two things
than dealing with the trade-offs
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
24. AGENDA
DDD Recap CQRS Step by Step
the strategic patterns probably simpler than you think! towards CQRS
1 2 3 4 5 6
βBest Practiceβ Domain Events End
what we call an Architecture are first class citizens Questions & Answers
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
26. Command Query Separation (CQS)
βEvery method should either be
a command that performs an action or
a query that returns data to the callerβ
-- Dr. Bertrand Meyer
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
27. Command Query Separation (CQS)
in other words:
Asking a question should
not change the answer
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
28. Command/Query
Responsability Segregation
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
29. CQS on an Architectural Level
Queries Commands
Project Details Project List Project
Data Store
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
30. Queries and Projection (Read)
Show something
Thin Read Layer
Data Store
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
31. Thin Read Layer
Concerns:
1 Filtering, Scope
2 Data Presentation
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
32. Thin Read Layer
Attributes:
1 Data-Oriented
2 Indexable
3 Can be denormalized and distributed for fast access
4 Only Structure
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
33. Behavior and Modification (Write)
Do something
Command Handler
Domain Model
O/R-Mapper
Data Store
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
34. Domain Model
Concerns:
1 Business Logic
2 Validation
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
35. Domain Model
Attributes:
1 Normalized
2 Transactional
3 Object-Oriented
4 Persistence Ignorant
5 Only Behavior
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
36. Commands
Refactor Application Service Calls to Objects
1 Serializable
2 Can be enveloped
3 Can be intercepted
4 Can be enriched
5 Communicate intent
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
37. Command Handlers
Refactored Application Service Methods:
1 Unify interface to all application services
2 Can be wrapped (e.g. Transaction, Authorization, Logging)
3 Can be enveloped (e.g. REST)
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
38. Command Handlers
Rule of thumb:
One Command Handler per use Case
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
39. This is CQRS
Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
Data Store
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
41. AGENDA
DDD Recap CQRS Step by Step
the strategic patterns probably simpler than you think! towards CQRS
1 2 3 4 5 6
βBest Practiceβ Domain Events End
what we call an Architecture are first class citizens Questions & Answers
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
43. Domain Event
captures the memory of
something interesting affecting the domain
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
44. Domain Event
the essence is to capture events that
trigger a change to the state
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
45. Domain Event
event objects are processed to
cause the respective change
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
46. Domain Event
and stored to provide an audit log
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
55. Added Added Added Removed
to to to from
Cart Cart Cart Cart
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
56. what if we
capture every single event?
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
57. β¦ and reproduce state from
this stream of events?
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
58. An Event Stream β¦
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
59. An Event Stream β¦
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Shopping
Cart
Can be projected into:
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
60. An Event Stream β¦
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Sales History
Shopping
Cart
β¦ or into this:
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
61. An Event Stream β¦
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Sales History
Audit Trail
Shopping
Cart
β¦ or into this:
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
62. An Event Stream β¦
Added Added Added Removed
to to to from
Cart Cart Cart Cart
Sales History
Audit Trail
Shopping
Campaign Effectiveness
Cart
β¦ or into this:
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
63. AGENDA
DDD Recap CQRS Step by Step
the strategic patterns probably simpler than you think! towards CQRS
1 2 3 4 5 6
βBest Practiceβ Domain Events End
what we call an Architecture are first class citizens Questions & Answers
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
64. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
Data Store
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
65. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
SQL
Data Store Views
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
66. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
INNER JOIN β¦
SQL LEFT OUTER JOIN β¦
Data Store Views UNION β¦
GROUP BY β¦
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
67. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
SELECT * FROM β¦
Table
Data Store per
Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
68. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
Table
Data Store per
Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
69. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
Event
Table
Data Store per
Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
70. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
Event
Event
Table
Data Store per
Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
71. Client
Commands Queries
Command Handlers
Domain Model
O/R Mapper Thin Read Layer
Event
Event
Event
Table
Data Store per
Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
73. Events
Domain Read Model
Commands DTOs
Client
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
74. Some Code
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
75. Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
76. Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
77. if ISBN exists in ShoppingCartDetails
Increase Amount
else
Add New Row
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
78. if Shopping Cart ID exists in ShoppingCartSummary
Increase NumberOfItems
Add OfferedPrice to TotalAmount
else
Add New Row
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
79. Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
80. Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
81. if Amount > 1 ShoppingCartDetails
Decrease Amount
else
Delete Row
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
82. Subtract OfferedPrice to TotalAmount
Decrease NumberOfItems
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
84. Testing the Write Model
Command Handlers
Domain Model
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
85. Testing the Write Model
Command Handlers
Given that certain Events happened
Domain Model
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
86. Testing the Write Model
When a specific Command is sent
Command Handlers
Given that certain Events happened
Domain Model
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
87. Testing the Write Model
When a specific Command is sent
Command Handlers
Given that certain Events happened
Domain Model
Event
Event
Then certain Events (and only those!) should be emitted
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
88. Testing the Read Model
Thin Read Layer
Table
per
Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
89. Testing the Read Model
Thin Read Layer
Given that certain Events happened
Table
per
Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
90. Testing the Read Model
Thin Read Layer
Given that certain Events happened
Table
per
When a specific Event happens Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
91. Testing the Read Model
Then each query should return the expected results
Thin Read Layer
Given that certain Events happened
Table
per
When a specific Event happens Query
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
93. Main Values
additive only
we donβt lose information
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
94. Main Values
linearly scalable and
distributable Read Model
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
95. Main Values
every new view can be created
from the beginning of time
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
96. Main Values
We can come up with new questions
at any time
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
97. Main Values
built-in integration model
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
99. mature business models
move away from Update/Delete
and become purely transactional
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
100. Human resources
Medicine
Bookkeeping
Banking
Finances
Government
β¦
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
101. AGENDA
DDD Recap CQRS Step by Step
the strategic patterns probably simpler than you think! towards CQRS
1 2 3 4 5 6
βBest Practiceβ Domain Events End
what we call an Architecture are first class citizens Questions & Answers
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
102. ?
QUESTIONS
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de
103. THANK YOU!
follow me: @dtraub
Dennis Traub β Software Development Consultancy @dtraub β mail@dennistraub.de