Upload
skills-matter
View
4.536
Download
1
Tags:
Embed Size (px)
Citation preview
CQRS & Domain Models
Udi Dahan – The Software Simplist Enterprise Development Expert & SOA Specialist
What most architecture looks like
Common Mistakes #1
Assuming that layers == tiers
We can deploy the same components both client-side and server-side
But where do business rules go?
Here?
Here?
Here?Here?
Here?
Here?
Code Reuse On A Pedestal
Understanding existing code takes longerthan writing new code
Too Much Of A “Good Thing”
For Example - CQRSQueries
CommandsDB
WSInput
Validation Validation
Rules
QueriesView Model
View ModelUpdater
PublishUI
Common Mistake #2
Assuming that all rules are equally likely to change
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
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!
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
Common Mistake #3
Assuming that race conditions affect the domain
Race Conditions
Ship OrderCancel Order
We think this should fail
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 );
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”
Aggregate Mistakes
public class Customer{
public void MakePreferred(){
foreach(Order o in this.UnbilledOrders)
o.Discount(10.Percent);}
}
In Closing
Avoid the common mistakes:#1 Layers & tiers#2 Rate of rule change#3 Race Conditions
Think about aggregates & bounded contexts
Thank youUdi Dahan – The Software SimplistEnterprise Development Expert & SOA Specialist
www.UdiDahan.com