ddd exchange 2010: udi dahan on architectural innovation: cqrs
TRANSCRIPT
![Page 1: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/1.jpg)
CQRS & Domain Models
Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist
![Page 2: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/2.jpg)
What most architecture looks like
![Page 3: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/3.jpg)
Common Mistakes #1
Assuming that layers == tiers
We can deploy the same components both client-side and server-side
![Page 4: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/4.jpg)
But where do business rules go?
Here?
Here?
Here?Here?
Here?
Here?
![Page 5: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/5.jpg)
Code Reuse On A Pedestal
Understanding existing code takes longerthan writing new code
![Page 6: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/6.jpg)
Too Much Of A “Good Thing”
![Page 7: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/7.jpg)
For Example - CQRSQueries
CommandsDB
WSInput
Validation Validation
Rules
QueriesView Model
View ModelUpdater
PublishUI
![Page 8: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/8.jpg)
Common Mistake #2
Assuming that all rules are equally likely to change
![Page 9: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/9.jpg)
Rules We Know How To Handle
In the domain model only:
When a customer cancels an order,if the order was already shipped,charge the customer $$unless customer is {some status}
UIServic
esBL
DAL
DB
![Page 10: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/10.jpg)
Rules We Struggle With
Where to enforce these rules?
- First name less than N chars- Username must be unique
UIServic
esBL
DAL
DBNot Domain Logic!
![Page 11: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/11.jpg)
The Domain ModelWhen To Use It & When Not To
“If you have complicated and everchanging business rules…”
“If you have simple not-null checks and a couple of sums to calculate, a Transaction Script is a better bet”
-- Martin Fowler, p119 Patterns of Enterprise Application Architecture
![Page 12: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/12.jpg)
Common Mistake #3
Assuming that race conditions affect the domain
![Page 13: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/13.jpg)
Race Conditions
Ship OrderCancel Order
We think this should fail
![Page 14: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/14.jpg)
What Domain Events Are For
What else needs to happen?
When a customer cancels an order,if the order was already shipped,charge the customer $$unless customer is {some status}
DomainEvents.Raise<InvokeCancellationFee>(f => f.OrderId = msg.OrderId );
![Page 15: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/15.jpg)
Consider The Reverse
Going to ship a cancelled order
Race condition between command & query
“One in a million” failure Let it fail asynchronously
Email back to the user“Sorry, that order doesn’t exist anymore”
![Page 16: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/16.jpg)
Aggregate Mistakes
public class Customer{
public void MakePreferred(){
foreach(Order o in this.UnbilledOrders)
o.Discount(10.Percent);}
}
![Page 17: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/17.jpg)
In Closing
Avoid the common mistakes:#1 Layers & tiers#2 Rate of rule change#3 Race Conditions
Think about aggregates & bounded contexts
![Page 18: DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS](https://reader034.vdocuments.net/reader034/viewer/2022051413/556646b9d8b42ab75e8b4720/html5/thumbnails/18.jpg)
Thank youUdi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist
www.UdiDahan.com