CQRS-Einführung - Teil 2
-
Upload
dennis-traub -
Category
Education
-
view
415 -
download
4
Transcript of CQRS-Einführung - Teil 2
CQRS – Eine EinführungPt. 2 – Event-Centric Architecture
.Net Online Usergroup – 20.02.2011
Presenter: Dennis Traub
Speaker, Trainer, Author, Software Developer since 1991
Twitter: @DTraub
CQRS Revisited
Domain Events
An Event-Centric Architecture
Some Code
What about Testing?
Summary
CQRS revisited
Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
CQRS Revisited
Domain Events
An Event-Centric Architecture
Some Code
What about Testing?
Summary
Definition: Domain Event
• Martin Fowler: (PEAA, 2002)
▫ Captures the memory of something interesting which affects the domain
▫ The essence […] is to capture things that can trigger a change to the state
▫ These event objects are then processed to cause changes to the system, and stored to provide an audit log
State
Shopping Cart
What we dont know:
What led to this state?
Addedto
Cart
Addedto
Cart
Addedto
Cart
Addedto
Cart
Addedto
Cart
Addedto
Cart
Addedto
Cart
Addedto
Cart
Addedto
Cart
RemovedfromCart
Which model contains
more Information?
This one?
Shopping Cart
Or this one?
Addedto
Cart
Addedto
Cart
Addedto
Cart
RemovedfromCart
What if we capture every single event?
And reproduce state from
this stream of events?
An Event Stream …
Addedto
Cart
Addedto
Cart
Addedto
Cart
RemovedfromCart
Shopping Cart
An Event Stream …
Addedto
Cart
Addedto
Cart
Addedto
Cart
RemovedfromCart
Can be projected into:
Shopping Cart
An Event Stream …
Addedto
Cart
Addedto
Cart
Addedto
Cart
RemovedfromCart
Sales History
… or into this:
Shopping Cart
An Event Stream …
Addedto
Cart
Addedto
Cart
Addedto
Cart
RemovedfromCart
Sales History
… or into this:
Audit Trail
Shopping Cart
An Event Stream …
Addedto
Cart
Addedto
Cart
Addedto
Cart
RemovedfromCart
Sales HistoryCampaign Effectiveness
… or into this:
Campaign Effectiveness
CQRS Revisited
Domain Events
Towards an Event-Centric Architecture
Some Code
What about Testing?
Summary
Simple CQRS
Relational Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
SQLViews
Optimized Write / Complex to Read
Relational Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
SQLViews
INNER JOIN …LEFT OUTER JOIN …UNION …GROUP BY …
INNER JOIN …LEFT OUTER JOIN …UNION …GROUP BY …
Optimized Write / Complex to Read
Relational Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
SQLViews
Replace Views with Table per Query
Relational Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
Tableper
Query
SELECT * FROM …
Emit Events when they occur
Relational Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
Tableper
View
Denormalize Events into Read Model
Relational Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
Tableper
View
Den
orm
ali
zer
Relational Data Storage
Thin Read LayerORM
Domain Model
Command Handlers
Client
Commands Queries
Tableper
View
Den
orm
ali
zer
Event Log
Add an Event Log for future retrieval
Simplified:
Client
Domain Read Model
DTOsCommands
Events
CQRS Revisited
Domain Events
Towards an Event-Centric Architecture
Some Code
What about Testing?
Summary
Denormalized Store:If ISBN exists in ShoppingCartDetails
Increase AmountElse
Add New Row
Denormalized Store:If Shopping Cart ID exists in ShoppingCartSummary
Increase Number of ItemsAdd Offered Price to Total Amount
ElseAdd New Row
Denormalized Store:If Amount > 1 in ShoppingCartDetails
Decrease AmountElse
Delete Row
Denormalized Store:
Subtract Offered Price from Total AmountDecrease Number of Items
CQRS Revisited
Domain Events
Towards an Event-Centric Architecture
Some Code
What about Testing?
Summary
Testing the Read Model
Thin Read Layer
Tableper
View
Den
orm
ali
zer
Testing the Read Model
Thin Read Layer
Tableper
View
Den
orm
ali
zer
Given that certain Events have occurred
Testing the Read Model
Thin Read Layer
Tableper
View
Den
orm
ali
zer
Given that certain Events have occurred
When a specific Event has occurred
Testing the Read Model
Thin Read Layer
Tableper
View
Den
orm
ali
zer
Given that certain Events have occurred
Then each query should produce the expected results
When a specific Event has occurred
Testing the Read Model
Given that a book with an ISBN of xxx and a price of USD 24.00 was added to a new shopping cart
When a book with an ISBN of yyy and a price of USD 19.00 was added to that same shopping cart
Then the Shopping Cart Summary should contain 2 booksand have a total amount of USD 43.00
And the Shopping Cart Details should contain book xxx
And the Shopping Cart Details should contain book yyy
Testing the Write Model
Domain Model
Command Handlers
Testing the Write Model
Domain Model
Command Handlers
Given that certain Events have occurred
Testing the Write Model
Domain Model
Command Handlers
CommandsWhen a specific Commands is sent
Given that certain Events have occurred
Testing the Write Model
Domain Model
Command Handlers
CommandsWhen a specific Commands is sent
Then the expected Events (and only those) should be emitted
Given that certain Events have occurred
Testing the Write Model
Given that an Add Book To Shopping Cart event withan ISBN of xxx had been sent
When a Remove Book From Shopping Cart command issent with an ISBN of zzz
Then a Book Removal Failed event should be emitted
And NO Book Was Removed From Shopping Cart eventshould be emitted
CQRS Revisited
Domain Events
Towards an Event-Centric Architecture
Some Code
What about Testing?
Summary
Main Value:
• Additive only, i.e. we don‘t lose information
• Linearly scalable and distributable Read Model
• Every new view can be created from the beginning of time
• We can come up with new questions at any time
• Built-In Integration Model
Proven Technology:
Mature business models move away from Update/Delete and become purely transactional:
▫ Human Resources
▫ Medicine
▫ Bookkeeping
▫ …
▫ Banking
▫ Financial Trading
▫ Government
What we haven‘t touched yet:
▫ Messaging
▫ Event Sourcing
▫ Sagas
▫ Eventual Consistency
▫ Integration Scenarios
▫ …
Resources
• http:// cqrs.wordpress.com
• http:// groups.google.com/group/dddcqrs
• http:// distributedpodcast.com
• http:// abdullin.com/cqrs
• http://cqrs.wikidot.com
Vielen Dank!
Dennis Traub@Dtraub
Please rate this talk at:
bit.ly/xyb4tx