Upload
prajeeshprathap
View
97
Download
1
Tags:
Embed Size (px)
Citation preview
Command/ Query Responsibility
Segregation
• How we started?• Distributed systems architecture• UI design• Domain model
What are we going to see
What not
And then came
And with it
Ultimate source of truth
• Paper was gone.• The source of truth changed
And then…
But
And
Distributed Architecture
Change
Data
Get D
ata
Get Data
User Collaboration
Users looking
into stale data
Cache
So why all the layers?
Cache
Relational data to DTO’s using ORMs
Map DTO’s to Domain Objects/ WS
Map from Domain Objects/ WS to View Models
Why transform between layers?
List of customers------------------------------------------------------------Data corrected as of 10 minutes
Be upfront about it
Only data, no behavior
No objects so why ORM’s
Query only
Persistent View Model
For each view in the UI have a table/ view in the DB
KISS
Search
• Find what the users want• Separate UI talking to a different store
No relations between Views/ Tables
Key
Value
Key
Value
Key
Value
Key
Value
Key
Value
Key
Value
Key
Value
Key
Value
Key
Value
Key
Value
Key
Value
Vs.
• Data duplication.• Avoid calculations on data.• Deploy to web tier (No need to hop
through firewalls, only SELECT is allowed)• Role-based security not needed.• Use for preliminary validation results in
less command rejection.
Data duplication
Commands
Can we fully trust our user decisions?
• Validations– Is the input potentially good– Structured correctly– Ranges, lengths etc.
• Rules– Should we do this?– Based on the current system state– What the user saw is irrelevant
Validations and Business Rules
• Easy versioning• Deployment
Autonomous components
Command Processing tiers
Capturing user intent
• Differentiating between– Correcting a shipping address – User has moved, rerouting shipment location
• Sometimes users accidently modify fields by tabbing.
Reservation systems
Reservation systems
• Checkbox based UI• Why user’s need bulk booking?• But then concurrency happens• Then book seats somewhere else
Not capturing user intent
Capturing user intent
• Group reservation• Enter number of people and preferred
seating location• System emails back when reservation is
confirmed
• On submit from browser, show comment using AJAX on page.
• Why wait for response from server.• Does the user calls all his friends and tell
them about the comment he posted?
Posting a comment on Facebook
DOMAIN MODEL
• Validations:– Commands are validated before the domain
model is called• Queries:
– Entity relations for reading are unnecessary
What aren’t they for
• In addition to doing what the command said, doing other things as well.
What are they for?
• The domain model is not responsible for persisting all the data. Only persist what is needed.
• The rest of the world is using the data from the query store anyway
Only persist what is needed
CQRS
Domain
Commands
Queries
PUTTING IT ALL TOGETHER
For better scalability, add pub/ sub