Upload
tom-janssens
View
741
Download
0
Embed Size (px)
DESCRIPTION
An intro into CQRS I gave for the Belgian Visual studio User group in March 2012
Citation preview
CQRS 101All your BASE belong to us
First dive into CQRS by Tom Janssens
March 6th, 2012
Hosted for VISUG.be by AE NV - Leuven, Belgium
Tom Janssens ● [email protected]● ToJans@twitter● http://www.corebvba.be/blog● http://github.com/ToJans
● Fun Fact: 10 startups in one year
Freelance problem solver - contact me !
Y U NO CQRS ?
● Occasionally disconnected client app● Google Groups Forum DDD/CQRS● CQRS articles on blog● OSS CQRS frameworks/libs:
Scritchy & MinimalisticCQRS● Member of MS P & P CQRS advisory board
A.K.A. my CQRS background and - experience
Overview Talk the talk● What● Why● When
Walk the walkDemo BankAccount● MinimalisticCQRS● Uniqueness ● Account transfer
WHAT: complexity evolutionSpaghetti approach
UI Data layer
WHAT: complexity evolutionLasagna approach *:
* Can contain additional layers
UI App layer Data layerDomain layer
Command: change state - push data (state changes)
Query: query state - pull data
WHAT: complexity evolution
* Command-Query separation - Bertrand Meyer"Asking a question should not change the answer"
CQS approach *:
UI App layer Data layer
Advantages: Scaling, risk, impact
UI App layer Data layerDomain layer
WHAT: complexity evolutionCQRS: Command Query Responsibility Segregation@Kellabyte on twitter
This is #CQRS http://bit.ly/zR2VuV That's it, that's all. What you put in behind those is entirely up to you.
WHAT: complexity evolution// CQS (Command-Query Separation)// Bertrand Meyer devised the CQS principle// [deleted for brevity]
public class CustomerService{ // Commands void MakeCustomerPreferred(CustomerId) void ChangeCustomerLocale(CustomerId, NewLocale) void CreateCustomer(Customer) void EditCustomerDetails(CustomerDetails)
// Queries Customer GetCustomer(CustomerId) CustomerSet GetCustomersWithName(Name) CustomerSet GetPreferredCustomers()}
WHAT: complexity evolution// CQRS (Command-Query Responsibility Segregation)// Defined by Greg Young.// [deleted for brevity]
public class CustomerWriteService{ // Commands void MakeCustomerPreferred(CustomerId) void ChangeCustomerLocale(CustomerId, NewLocale) void CreateCustomer(Customer) void EditCustomerDetails(CustomerDetails)}
public class CustomerReadService{ // Queries Customer GetCustomer(CustomerId) CustomerSet GetCustomersWithName(Name) CustomerSet GetPreferredCustomers()}
That's all folks !!! The End !... Or not ?
Command: change state - push data (state changes)
Query: build/query state - pull data
WHAT: complexity evolution
* Command-Query Responsibility Segregation - Greg Young, Udi Dahan
CQRS approach *:
"The creation of 2 objects where there previously was one"
EventsUI Data layer
UI Domain layerCommandsEvents
buildquery
What: messagesCommand● DoStuff !● Imperative
void DoStuff( ... ){ if (somethingtrue) apply(StuffDone);}
Event● StuffDone● Past tense
void Handle(StuffDone){ SomeState = true;}
IMPORTANT!!An event handler should ALWAYS have an immutable execution path,i.e. no logic !!
Command: change state - push data (state changes)
Query: build/query state - pull data
WHAT: complexity evolutionCQRS approach *:
Example code : Scritchy example
EventsUI Data layer
UI Domain layerCommandsEvents
buildquery
Why ?Clear separation of concerns
Domain
Query side
UICommands
Events
update view
models
"Circular architecture" Evolution of Alister Cockburn's hexagonal architecture; no more adapters needed
WhyClear separation of concerns● Requires different knowledge/other devs● Testability● Ubiquitous language● New business requirements
WhyEvent-driven architecture and the cloud: CAP theorem: pick 2
Consistency
Availability Partitionability
Why: events & the cloudACID : Consistency & Availability● Atomicity
All operations complete, or none● Consistency
(During a transaction)● Isolation
Every operation behaves as it is the only one● Durability
Finished tx will not be reversed
Why: events & the cloudBASE : Availability & Partitionability● Basically
Available● Soft state● Eventually consistent=> CLOUD !!!
=> Solution for the consistency boundariesDDD: AR's & sagas
Why: events & the cloud
Why: events & the cloud
Why: events & the cloud
Why: events & the cloud
When to use CQRS ?Only apply it to the parts of your problem domain that offer you a competitive advantage.
Indicator for Complexity/uniqueness:=> T-shirt categories (Greg Young)
CRUD - tell what can't be doneDDD - tell what can be done
ref: free eBook "DDD quickly","What is DDD?"
Works really well with DDD/ES=> When your problem domains are not fixed
BreakUp next - demo live coding
Conclusion
What:
Two objects where there previously was one
Remove layers and abstractions
Circular architecture
=> Fire and forget
Conclusion
Why:
Separation of concerns
Event driven architecture
Flexibility, Scalability
Testability
Conclusion
When:
Complexity
Uniqueness
Competitive advantage
Parts of your problem domain
Thanks
CQRS folks - for being such an avid community
@YReynhout and @Abdullin for review
VISUG.be and AE NV for hosting the event
Questions & feedback ?Twitter: @ToJans