CQRS & Domain Models<br />Udi Dahan – The Software Simplist<br />Enterprise Development Expert & SOA Specialist<br />
What most architecture looks like<br />
Common Mistakes #1<br />Assuming that layers == tiers<br />We can deploy the same components both client-side and server-s...
But where do business rules go?<br />Here?<br />Here?<br />Here?<br />Here?<br />Here?<br />Here?<br />
Code Reuse On A Pedestal<br />Understanding existing code takes longer<br />than writing new code<br />
Too Much Of A “Good Thing”<br />
For Example - CQRS<br />Queries<br />View Model<br />UI<br />Queries<br />Commands<br />Publish<br />WS<br />Input<br />Va...
Common Mistake #2<br />Assuming that all rules are equally likely to change<br />
Rules We Know How To Handle <br />In the domain model only:<br />When a customer cancels an order,<br />	if the order was ...
Rules We Struggle With<br />Where to enforce these rules?<br />- First name less than N chars<br />	- Username must be uni...
The Domain Model	When To Use It  &  When Not To<br />“If you have complicated and everchanging business rules…”<br />“If y...
Common Mistake #3<br />Assuming that race conditions affect the domain<br />
Race Conditions<br />Cancel Order<br />Ship Order<br />We think this should fail<br />
What Domain Events Are For<br />What else needs to happen?<br />	When a customer cancels an order,<br />	if the order was ...
Consider The Reverse<br />Going to ship a cancelled order<br />Race condition between command & query<br />“One in a milli...
Aggregate Mistakes<br />public class Customer<br />{<br />publicvoidMakePreferred()<br />	{<br />foreach(Ordero inthis.Unb...
In Closing<br />Avoid the common mistakes:<br />	#1  Layers & tiers<br />	#2  Rate of rule change<br />	#3  Race Condition...
Thank you<br />Udi Dahan – The Software Simplist<br />Enterprise Development Expert & SOA Specialist<br />www.UdiDahan.com...
Upcoming SlideShare
Loading in...5
×

DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

4,561

Published on

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
  • For more details and podcasts from the DDD eXchange 2010 go to http://skillsmatter.com/event-details/podcast/ddd-exchange-2010
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
4,561
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
141
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • Same rules for insurance, finance, retail, online gambling, etc.These are technical constraints.We’d expect domain logic like in the previous slide to change often, these rules hardly ever change.
  • Thousands of years of paper-based business worked around this just fine, even though the latency increased the chance of these “concurrency conflict”
  • DDD eXchange 2010: Udi Dahan on Architectural Innovation: CQRS

    1. 1. CQRS & Domain Models<br />Udi Dahan – The Software Simplist<br />Enterprise Development Expert & SOA Specialist<br />
    2. 2. What most architecture looks like<br />
    3. 3. Common Mistakes #1<br />Assuming that layers == tiers<br />We can deploy the same components both client-side and server-side<br />
    4. 4. But where do business rules go?<br />Here?<br />Here?<br />Here?<br />Here?<br />Here?<br />Here?<br />
    5. 5. Code Reuse On A Pedestal<br />Understanding existing code takes longer<br />than writing new code<br />
    6. 6. Too Much Of A “Good Thing”<br />
    7. 7. For Example - CQRS<br />Queries<br />View Model<br />UI<br />Queries<br />Commands<br />Publish<br />WS<br />Input<br />Validation<br />Validation<br />Rules<br />View Model<br />Updater<br />DB<br />
    8. 8. Common Mistake #2<br />Assuming that all rules are equally likely to change<br />
    9. 9. Rules We Know How To Handle <br />In the domain model only:<br />When a customer cancels an order,<br /> if the order was already shipped,<br /> charge the customer $$<br /> unless customer is {some status}<br />DB<br />
    10. 10. Rules We Struggle With<br />Where to enforce these rules?<br />- First name less than N chars<br /> - Username must be unique<br />Not Domain Logic!<br />DB<br />
    11. 11. The Domain Model When To Use It & When Not To<br />“If you have complicated and everchanging business rules…”<br />“If you have simple not-null checks and a couple of sums to calculate, a Transaction Script is a better bet”<br /> -- Martin Fowler, p119<br />Patterns of Enterprise Application Architecture<br />
    12. 12. Common Mistake #3<br />Assuming that race conditions affect the domain<br />
    13. 13. Race Conditions<br />Cancel Order<br />Ship Order<br />We think this should fail<br />
    14. 14. What Domain Events Are For<br />What else needs to happen?<br /> When a customer cancels an order,<br /> if the order was already shipped,<br /> charge the customer $$<br /> unless customer is {some status}<br />DomainEvents.Raise<InvokeCancellationFee>(<br /> f => f.OrderId = msg.OrderId );<br />
    15. 15. Consider The Reverse<br />Going to ship a cancelled order<br />Race condition between command & query<br />“One in a million” failure<br /> Let it fail asynchronously<br />Email back to the user<br /> “Sorry, that order doesn’t exist anymore”<br />
    16. 16. Aggregate Mistakes<br />public class Customer<br />{<br />publicvoidMakePreferred()<br /> {<br />foreach(Ordero inthis.UnbilledOrders)<br />o.Discount(10.Percent);<br /> }<br />}<br />
    17. 17. In Closing<br />Avoid the common mistakes:<br /> #1 Layers & tiers<br /> #2 Rate of rule change<br /> #3 Race Conditions<br />Think about aggregates & bounded contexts<br />
    18. 18. Thank you<br />Udi Dahan – The Software Simplist<br />Enterprise Development Expert & SOA Specialist<br />www.UdiDahan.com<br />email@UdiDahan.com<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×