Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CQRS
IN 4 STEPS
WHAT IS THE
COMMAND QUERY RESPONSIBILITY
SEGREGATION
What is not required in CQRS
• More than 1 data source
• Domain events
• Event Sourcing
• Message buses
• And others
0
RECENT STATE
Ecommerce app
ProductsServiceProductsController
OrdersServiceOrdersController
Ecommerce app - NopCommerce
Disadvantages of this approach
• Enormous controllers / services / helpers
• Classes are not following SRP
• Refactoring i...
1
CQRS IS TWO OBJECTS
WHERE THERE WAS ONCE ONE
Jimmy Bogard - https://lostechies.com/jimmybogard/2012/08/22/busting-some-c...
Martin Fowler - https://martinfowler.com/bliki/CQRS.html
Step 1 – Splitting to objects
GetProducts
QueryHandler
ProductsController
AddProduct
CommandHandler
Mediator
Jimmy Bogard ...
Profit after this step
• Single responsibility
• Clearly defined target and dependencies
• Split to change and receive dat...
2
BOOSTING
YOUR QUERIES
Many objects
GetProductsQueryHandle
r
ProductsController
AddProductCommandHandler
CategoriesController
GetProductsFieldsQu...
Objects without change
AddProductCommandHandler
GetProductsFieldsQueryHandler
AddCategoryCommandHandler
Step 2 – Request optimization
GetProductsQueryHandler
GetCategoriesQueryHandler
GetCategoriesProductsQueryHandle
r
X
Plenty of options
SQL Views
Materialized
AutoMapper
ProjectTo
Dapper
MicroORM
EF No tracking
OrmLite
MicroORM
SQL Queries
Profit after this step
• Improved speed of receiving data
• No impact on other parts of the system
3
CREATING SYNCHRONOUS
READMODEL
Publishing domain events
ChangeProductField
CommandHandler
Mediator
AddProduct
CommandHandler
Mediator
ProductAdded
Produc...
Step 3 – Synchronous read model
ProductAdded
ProductFieldChanged
Product
View
Handler
ProductView
(table)
Profit after this step
• Iterative gain in read model
• Immediate queries
• No data loss
• Ability to recreate a view
4
CREATING ASYNCHRONOUS
READMODEL
Message bus
ProductAdded
ProductFieldChanged
Async
Job
Step 4 – Asynchronous read model
Product
Search
Handler
Async
Job
Product
Cache
Handler
Notify the user - SignalR
Product
Search
Handler
Async
Job
Products
Controller
SignalR
Correlation Identifier - http://www...
Profit after this step
• Read model per functionality
• Non-blockable commands
• Small, testable components
Summary
• 1 – split to objects: Command & Query
• 2 – query optimization
• 3 – synchronous read model
• 4 – asynchronous r...
THANK YOU
CQRS in 4 steps - Lightning Talk
CQRS in 4 steps - Lightning Talk
CQRS in 4 steps - Lightning Talk
CQRS in 4 steps - Lightning Talk
CQRS in 4 steps - Lightning Talk
Upcoming SlideShare
Loading in …5
×

CQRS in 4 steps - Lightning Talk

807 views

Published on

This is a presentation how to introduce CQRS pattern to an existing application, step by step, without breaking changes and holding up the development.

Published in: Internet
  • Be the first to comment

CQRS in 4 steps - Lightning Talk

  1. 1. CQRS IN 4 STEPS
  2. 2. WHAT IS THE COMMAND QUERY RESPONSIBILITY SEGREGATION
  3. 3. What is not required in CQRS • More than 1 data source • Domain events • Event Sourcing • Message buses • And others
  4. 4. 0 RECENT STATE
  5. 5. Ecommerce app ProductsServiceProductsController OrdersServiceOrdersController
  6. 6. Ecommerce app - NopCommerce
  7. 7. Disadvantages of this approach • Enormous controllers / services / helpers • Classes are not following SRP • Refactoring is really difficult • Complexity in writing unit tests
  8. 8. 1 CQRS IS TWO OBJECTS WHERE THERE WAS ONCE ONE Jimmy Bogard - https://lostechies.com/jimmybogard/2012/08/22/busting-some-cqrs-myths/
  9. 9. Martin Fowler - https://martinfowler.com/bliki/CQRS.html
  10. 10. Step 1 – Splitting to objects GetProducts QueryHandler ProductsController AddProduct CommandHandler Mediator Jimmy Bogard - https://lostechies.com/jimmybogard/2015/05/05/cqrs-with-mediatr-and-automapper/
  11. 11. Profit after this step • Single responsibility • Clearly defined target and dependencies • Split to change and receive data • Simplicity of writing unit tests
  12. 12. 2 BOOSTING YOUR QUERIES
  13. 13. Many objects GetProductsQueryHandle r ProductsController AddProductCommandHandler CategoriesController GetProductsFieldsQueryHandler GetCategoriesQueryHandle r AddCategoryCommandHandler GetCategoriesProductsQueryHandle r
  14. 14. Objects without change AddProductCommandHandler GetProductsFieldsQueryHandler AddCategoryCommandHandler
  15. 15. Step 2 – Request optimization GetProductsQueryHandler GetCategoriesQueryHandler GetCategoriesProductsQueryHandle r X
  16. 16. Plenty of options SQL Views Materialized AutoMapper ProjectTo Dapper MicroORM EF No tracking OrmLite MicroORM SQL Queries
  17. 17. Profit after this step • Improved speed of receiving data • No impact on other parts of the system
  18. 18. 3 CREATING SYNCHRONOUS READMODEL
  19. 19. Publishing domain events ChangeProductField CommandHandler Mediator AddProduct CommandHandler Mediator ProductAdded ProductFieldChanged Udi Dahan - http://udidahan.com/2009/06/14/domain-events-salvation/
  20. 20. Step 3 – Synchronous read model ProductAdded ProductFieldChanged Product View Handler ProductView (table)
  21. 21. Profit after this step • Iterative gain in read model • Immediate queries • No data loss • Ability to recreate a view
  22. 22. 4 CREATING ASYNCHRONOUS READMODEL
  23. 23. Message bus ProductAdded ProductFieldChanged Async Job
  24. 24. Step 4 – Asynchronous read model Product Search Handler Async Job Product Cache Handler
  25. 25. Notify the user - SignalR Product Search Handler Async Job Products Controller SignalR Correlation Identifier - http://www.enterpriseintegrationpatterns.com/patterns/messaging/CorrelationIdentifier.html
  26. 26. Profit after this step • Read model per functionality • Non-blockable commands • Small, testable components
  27. 27. Summary • 1 – split to objects: Command & Query • 2 – query optimization • 3 – synchronous read model • 4 – asynchronous read model & notifications
  28. 28. THANK YOU

×