Command Query Responsibility Segregation (CQRS)

4,279 views

Published on

CQRS Tech Talk @ Windsor Hackforge

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,279
On SlideShare
0
From Embeds
0
Number of Embeds
1,906
Actions
Shares
0
Downloads
36
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Command Query Responsibility Segregation (CQRS)

  1. 1. Command Query Responsibility Segregation (CQRS) Derek Comartin @derek_comartin CodeOpinion.com
  2. 2. Agenda • • • • • • • Why? Typical Architecture What is CQRS How to apply CQRS Other benefits Why Not? Takeaways
  3. 3. Why? • • • • • • Complex Domain Read & Write Boundaries Divergent Change Scalability Skill Set Segregation Architecture Choices
  4. 4. Typical Architecture Data Storage Domain Object Domain Object Application Service Facade DTO DTO Client
  5. 5. Typical Architecture - Client Side 6 Ack/Nak Response 1 5 Request DTO 2 Send DTO to Server Display DTO in View 3 4 Modify/Rebuild DTO
  6. 6. XML of DTO <BankAccount> <Id>123</Id> <CustomerId>456</CustomerId> <Type>Savings</Type> <Overdraft>200.00</Overdraft> <InterestRate>0.0035</InterestRate> <Active>True</Active> </BankAccount>
  7. 7. Typical Architecture - Analysis • • • • • Simple & Common Tooling: ActiveRecord, ORM, AutoMapper. Data Driven Potentially Anemic Domain Model Hard to scale data storage
  8. 8. Command Query Separation Every method should either be a command that performs an action, or a query that returns data to the caller, but not both.
  9. 9. Command Query Responsibility Segregation ”CQRS is simply the creation of two objects where there was previously only one.” -Greg Young
  10. 10. Typical Service BankAccountService { BankAccount CreateBankAccount(BankAccount); BankAccount GetBankAccount(AccountId); decimal UpdateBalance(AccountId, Balance); void UpdateBankAccount(BankAccount); decimal GetBankAccountBalance(AccountId); }
  11. 11. CQRS Services BankAccountReadService { BankAccount GetBankAccount(AccountId); decimal GetBankAccountBalance(AccountId); } BankAccountWriteService { BankAccount CreateBankAccount(BankAccount); decimal UpdateBalance(AccountId, Balance); void UpdateBankAccount(BankAccount); }
  12. 12. "Defining the CQRS pattern is easy. Realizing the benefits that implementing the CQRS pattern can offer is not always so straightforward." – Microsoft Patterns & Practices "This simple notion leads to some profound consequences for the design of information systems." –Martin Fowler
  13. 13. Complex Domain • • • (Distributed) Domain Driven Design Behavior Intent
  14. 14. Typical Architecture Data Storage Domain Object Domain Object Application Service Facade DTO DTO Client
  15. 15. Simple CQRS Data Storage Domain Object Domain Object Application Service Read Layer Facade Facade Query DTO Command DTO Client
  16. 16. CQRS - Client Side 6 Ack/Nak Response (optional) 1 5 Request DTO 2 Send Command Object to Server Display DTO in View 3 4 Build Command Object
  17. 17. Command = Intent DepositCommand { Guid AccountId; decimal DepositAmount; }
  18. 18. CQRS Services BankAccountReadService { BankAccountViewModel GetBankAccount(AccountId); decimal GetBankAccountBalance(AccountId); } BankAccountWriteService { void Send(OpenBankAccountCommand); void Send(DepositCommand); void Send(WithdrawlCommand); void Send(DeactivateCommand); }
  19. 19. More Options • • • Multiple Models Domain Model = 3rd Normal Form Read Model = 1st Normal Form
  20. 20. Read & Write Boundaries Divergent Change Data Storage Domain Object Domain Object Application Service Read Layer Facade Facade Query DTO Command DTO Client
  21. 21. Skill Set Segregation Data Storage Domain Object Domain Object Application Service Read Layer Facade Facade Query DTO Command DTO Client
  22. 22. Scalability Event Storage Message Queue Event Handlers Events Data Storage Domain Object Data Storage Domain Object Command Handlers Read Layer Facade Message Queue Command DTO Query DTO Client
  23. 23. Notes • • • CRUD (not with DDD) Not top level architecture Determine per bounded context
  24. 24. Takeaways • • • Separate reads and writes between two objects. That’s it. Everything else (DDD, Event Sourcing, Messaging…) is not CQRS. They just fit extremely well.
  25. 25. Resources Greg Young @gregyoung goodenoughsoftware.net Udi Dahan @udidahan udidahan.com Microsoft Patterns & Practices CQRS Journey

×