30
CQRS 101 All your BASE belong to us First dive into CQRS by Tom Janssens March 6th, 2012 Hosted for VISUG.be by AE NV - Leuven, Belgium

Cqrs 101 all your base belong to us

Embed Size (px)

DESCRIPTION

An intro into CQRS I gave for the Belgian Visual studio User group in March 2012

Citation preview

Page 1: Cqrs 101  all your base belong to us

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

Page 2: Cqrs 101  all your base belong to us

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 !

Page 3: Cqrs 101  all your base belong to us

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

Page 4: Cqrs 101  all your base belong to us

Overview Talk the talk● What● Why● When

Walk the walkDemo BankAccount● MinimalisticCQRS● Uniqueness ● Account transfer

Page 5: Cqrs 101  all your base belong to us

WHAT: complexity evolutionSpaghetti approach

UI Data layer

Page 6: Cqrs 101  all your base belong to us

WHAT: complexity evolutionLasagna approach *:

* Can contain additional layers

UI App layer Data layerDomain layer

Page 7: Cqrs 101  all your base belong to us

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

Page 8: Cqrs 101  all your base belong to us

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.

Page 9: Cqrs 101  all your base belong to us

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()}

Page 10: Cqrs 101  all your base belong to us

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()}

Page 11: Cqrs 101  all your base belong to us

That's all folks !!! The End !... Or not ?

Page 12: Cqrs 101  all your base belong to us

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

Page 13: Cqrs 101  all your base belong to us

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 !!

Page 14: Cqrs 101  all your base belong to us

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

Page 15: Cqrs 101  all your base belong to us

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

Page 16: Cqrs 101  all your base belong to us

WhyClear separation of concerns● Requires different knowledge/other devs● Testability● Ubiquitous language● New business requirements

Page 17: Cqrs 101  all your base belong to us

WhyEvent-driven architecture and the cloud: CAP theorem: pick 2

Consistency

Availability Partitionability

Page 18: Cqrs 101  all your base belong to us

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

Page 19: Cqrs 101  all your base belong to us

Why: events & the cloudBASE : Availability & Partitionability● Basically

Available● Soft state● Eventually consistent=> CLOUD !!!

=> Solution for the consistency boundariesDDD: AR's & sagas

Page 20: Cqrs 101  all your base belong to us

Why: events & the cloud

Page 21: Cqrs 101  all your base belong to us

Why: events & the cloud

Page 22: Cqrs 101  all your base belong to us

Why: events & the cloud

Page 23: Cqrs 101  all your base belong to us

Why: events & the cloud

Page 24: Cqrs 101  all your base belong to us

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

Page 25: Cqrs 101  all your base belong to us

BreakUp next - demo live coding

Page 26: Cqrs 101  all your base belong to us

Conclusion

What:

Two objects where there previously was one

Remove layers and abstractions

Circular architecture

=> Fire and forget

Page 27: Cqrs 101  all your base belong to us

Conclusion

Why:

Separation of concerns

Event driven architecture

Flexibility, Scalability

Testability

Page 28: Cqrs 101  all your base belong to us

Conclusion

When:

Complexity

Uniqueness

Competitive advantage

Parts of your problem domain

Page 29: Cqrs 101  all your base belong to us

Thanks

CQRS folks - for being such an avid community

@YReynhout and @Abdullin for review

VISUG.be and AE NV for hosting the event

Page 30: Cqrs 101  all your base belong to us

Questions & feedback ?Twitter: @ToJans