An Introduction to CQRS

594 views
535 views

Published on

Presented at dotnetkk.de on March 20th 2013 by Dennis Traub

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
  • A bit consused that it's the same presentation as http://www.slideshare.net/Dennis_Traub/from-ddd-to-cqrs. An outstanding one, though!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
594
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
0
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

An Introduction to CQRS

  1. 1. An Introduction to Command/Query Responsibility Segregation presented by Dennis Traub .NET Stammtisch Konstanz/Kreuzlingen - 20. März 2013Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  2. 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 @dtraubDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  3. 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 & AnswersDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  4. 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 & AnswersDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  5. 5. The „Blue Book“ by Eric Evans Image PlaceholderDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  6. 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. 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. 8. don‘t build one big model that works for everyoneDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  9. 9. the tactical Building Blocks we don‘t use DDD when there is no value in formalizing the problemDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  10. 10. we only use DDD in parts where we get a competitive advantageDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  11. 11. we focus our effort and resources on the most important subdomainDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  12. 12. a.k.a. The Core DomainDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  13. 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 & AnswersDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  14. 14. Presentation Layer Application Services Domain / Business Model / BLL DAL / O/R-MapperDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  15. 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 limitiationsDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  16. 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 stuffDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  17. 17. And: If the model is basically CRUD where does Business Logic live?Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  18. 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 LoadingDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  19. 19. the S in SOLIDDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  20. 20. SINGLE RESPONSIBILITY PRINCIPLE Just because you can doesn‘t mean you shouldDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  21. 21. why do we try to build One Model to Rule Them All?Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  22. 22. reading and writing are completely different from eachotherDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  23. 23. Sometimes it‘s cheaper to do two things than dealing with the trade-offsDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  24. 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 & AnswersDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  25. 25. Command Query SeparationDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  26. 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 MeyerDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  27. 27. Command Query Separation (CQS) in other words: Asking a question should not change the answerDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  28. 28. Command/Query Responsability SegregationDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  29. 29. CQS on an Architectural Level Queries Commands Project Details Project List Project Data StoreDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  30. 30. Queries and Projection (Read) Show something Thin Read Layer Data StoreDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  31. 31. Thin Read Layer Concerns: 1 Filtering, Scope 2 Data PresentationDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  32. 32. Thin Read Layer Attributes: 1 Data-Oriented 2 Indexable 3 Can be denormalized and distributed for fast access 4 Only StructureDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  33. 33. Behavior and Modification (Write) Do something Command Handler Domain Model O/R-Mapper Data StoreDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  34. 34. Domain Model Concerns: 1 Business Logic 2 ValidationDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  35. 35. Domain Model Attributes: 1 Normalized 2 Transactional 3 Object-Oriented 4 Persistence Ignorant 5 Only BehaviorDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  36. 36. Commands Refactor Application Service Calls to Objects 1 Serializable 2 Can be enveloped 3 Can be intercepted 4 Can be enriched 5 Communicate intentDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  37. 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. 38. Command Handlers Rule of thumb: One Command Handler per use CaseDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  39. 39. This is CQRS Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Data StoreDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  40. 40. BREAKDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  41. 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 & AnswersDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  42. 42. Domain EventDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  43. 43. Domain Event captures the memory of something interesting affecting the domainDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  44. 44. Domain Event the essence is to capture events that trigger a change to the stateDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  45. 45. Domain Event event objects are processed to cause the respective changeDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  46. 46. Domain Event and stored to provide an audit logDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  47. 47. Shopping CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  48. 48. we don‘t know: What led to this state?Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  49. 49. Added to CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  50. 50. Added Added to to Cart CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  51. 51. Added Added Added to to to Cart Cart CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  52. 52. Added Added Added Removed to to to from Cart Cart Cart CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  53. 53. Which model contains more information?Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  54. 54. Shopping CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  55. 55. Added Added Added Removed to to to from Cart Cart Cart CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  56. 56. what if we capture every single event?Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  57. 57. … and reproduce state from this stream of events?Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  58. 58. An Event Stream … Added Added Added Removed to to to from Cart Cart Cart CartDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  59. 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. 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. 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. 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. 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 & AnswersDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  64. 64. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Data StoreDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  65. 65. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer SQL Data Store ViewsDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  66. 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. 67. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer SELECT * FROM … Table Data Store per QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  68. 68. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Table Data Store per QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  69. 69. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Event Table Data Store per QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  70. 70. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Event Event Table Data Store per QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  71. 71. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Event Event Event Table Data Store per QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  72. 72. Simplified:Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  73. 73. Events Domain Read Model Commands DTOs ClientDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  74. 74. Some CodeDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  75. 75. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  76. 76. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  77. 77. if ISBN exists in ShoppingCartDetails Increase Amount else Add New RowDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  78. 78. if Shopping Cart ID exists in ShoppingCartSummary Increase NumberOfItems Add OfferedPrice to TotalAmount else Add New RowDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  79. 79. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  80. 80. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  81. 81. if Amount > 1 ShoppingCartDetails Decrease Amount else Delete RowDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  82. 82. Subtract OfferedPrice to TotalAmount Decrease NumberOfItemsDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  83. 83. TestabilityDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  84. 84. Testing the Write Model Command Handlers Domain ModelDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  85. 85. Testing the Write Model Command Handlers Given that certain Events happened Domain ModelDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  86. 86. Testing the Write Model When a specific Command is sent Command Handlers Given that certain Events happened Domain ModelDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  87. 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 emittedDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  88. 88. Testing the Read Model Thin Read Layer Table per QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  89. 89. Testing the Read Model Thin Read Layer Given that certain Events happened Table per QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  90. 90. Testing the Read Model Thin Read Layer Given that certain Events happened Table per When a specific Event happens QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  91. 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 QueryDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  92. 92. Main ValuesDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  93. 93. Main Values additive only we don‘t lose informationDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  94. 94. Main Values linearly scalable and distributable Read ModelDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  95. 95. Main Values every new view can be created from the beginning of timeDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  96. 96. Main Values We can come up with new questions at any timeDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  97. 97. Main Values built-in integration modelDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  98. 98. Proven TechnologyDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  99. 99. mature business models move away from Update/Delete and become purely transactionalDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  100. 100. Human resources Medicine Bookkeeping Banking Finances Government …Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  101. 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 & AnswersDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  102. 102. ? QUESTIONSDennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  103. 103. THANK YOU!Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de

×