cqrs-einführung - teil 2

56
CQRS Eine Einführung Pt. 2 Event-Centric Architecture .Net Online Usergroup 20.02.2011 Presenter: Dennis Traub Speaker, Trainer, Author, Software Developer since 1991 Twitter: @DTraub

Upload: dennis-traub

Post on 09-Jul-2015

415 views

Category:

Education


4 download

TRANSCRIPT

Page 1: 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

Page 2: CQRS-Einführung - Teil 2

CQRS Revisited

Domain Events

An Event-Centric Architecture

Some Code

What about Testing?

Summary

Page 3: CQRS-Einführung - Teil 2

CQRS revisited

Data Storage

Thin Read LayerORM

Domain Model

Command Handlers

Client

Commands Queries

Page 4: CQRS-Einführung - Teil 2

CQRS Revisited

Domain Events

An Event-Centric Architecture

Some Code

What about Testing?

Summary

Page 5: CQRS-Einführung - Teil 2

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

Page 6: CQRS-Einführung - Teil 2

State

Shopping Cart

Page 7: CQRS-Einführung - Teil 2

What we dont know:

What led to this state?

Page 8: CQRS-Einführung - Teil 2

Addedto

Cart

Page 9: CQRS-Einführung - Teil 2

Addedto

Cart

Addedto

Cart

Page 10: CQRS-Einführung - Teil 2

Addedto

Cart

Addedto

Cart

Addedto

Cart

Page 11: CQRS-Einführung - Teil 2

Addedto

Cart

Addedto

Cart

Addedto

Cart

RemovedfromCart

Page 12: CQRS-Einführung - Teil 2

Which model contains

more Information?

Page 13: CQRS-Einführung - Teil 2

This one?

Shopping Cart

Page 14: CQRS-Einführung - Teil 2

Or this one?

Addedto

Cart

Addedto

Cart

Addedto

Cart

RemovedfromCart

Page 15: CQRS-Einführung - Teil 2

What if we capture every single event?

Page 16: CQRS-Einführung - Teil 2

And reproduce state from

this stream of events?

Page 17: CQRS-Einführung - Teil 2

An Event Stream …

Addedto

Cart

Addedto

Cart

Addedto

Cart

RemovedfromCart

Page 18: CQRS-Einführung - Teil 2

Shopping Cart

An Event Stream …

Addedto

Cart

Addedto

Cart

Addedto

Cart

RemovedfromCart

Can be projected into:

Page 19: CQRS-Einführung - Teil 2

Shopping Cart

An Event Stream …

Addedto

Cart

Addedto

Cart

Addedto

Cart

RemovedfromCart

Sales History

… or into this:

Page 20: CQRS-Einführung - Teil 2

Shopping Cart

An Event Stream …

Addedto

Cart

Addedto

Cart

Addedto

Cart

RemovedfromCart

Sales History

… or into this:

Audit Trail

Page 21: CQRS-Einführung - Teil 2

Shopping Cart

An Event Stream …

Addedto

Cart

Addedto

Cart

Addedto

Cart

RemovedfromCart

Sales HistoryCampaign Effectiveness

… or into this:

Campaign Effectiveness

Page 22: CQRS-Einführung - Teil 2

CQRS Revisited

Domain Events

Towards an Event-Centric Architecture

Some Code

What about Testing?

Summary

Page 23: CQRS-Einführung - Teil 2

Simple CQRS

Relational Data Storage

Thin Read LayerORM

Domain Model

Command Handlers

Client

Commands Queries

SQLViews

Page 24: CQRS-Einführung - Teil 2

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 …

Page 25: CQRS-Einführung - Teil 2

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

Page 26: CQRS-Einführung - Teil 2

Replace Views with Table per Query

Relational Data Storage

Thin Read LayerORM

Domain Model

Command Handlers

Client

Commands Queries

Tableper

Query

SELECT * FROM …

Page 27: CQRS-Einführung - Teil 2

Emit Events when they occur

Relational Data Storage

Thin Read LayerORM

Domain Model

Command Handlers

Client

Commands Queries

Tableper

View

Page 28: CQRS-Einführung - Teil 2

Denormalize Events into Read Model

Relational Data Storage

Thin Read LayerORM

Domain Model

Command Handlers

Client

Commands Queries

Tableper

View

Den

orm

ali

zer

Page 29: CQRS-Einführung - Teil 2

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

Page 30: CQRS-Einführung - Teil 2

Simplified:

Client

Domain Read Model

DTOsCommands

Events

Page 31: CQRS-Einführung - Teil 2

CQRS Revisited

Domain Events

Towards an Event-Centric Architecture

Some Code

What about Testing?

Summary

Page 32: CQRS-Einführung - Teil 2
Page 33: CQRS-Einführung - Teil 2
Page 34: CQRS-Einführung - Teil 2
Page 35: CQRS-Einführung - Teil 2

Denormalized Store:If ISBN exists in ShoppingCartDetails

Increase AmountElse

Add New Row

Page 36: CQRS-Einführung - Teil 2

Denormalized Store:If Shopping Cart ID exists in ShoppingCartSummary

Increase Number of ItemsAdd Offered Price to Total Amount

ElseAdd New Row

Page 37: CQRS-Einführung - Teil 2
Page 38: CQRS-Einführung - Teil 2

Denormalized Store:If Amount > 1 in ShoppingCartDetails

Decrease AmountElse

Delete Row

Page 39: CQRS-Einführung - Teil 2

Denormalized Store:

Subtract Offered Price from Total AmountDecrease Number of Items

Page 40: CQRS-Einführung - Teil 2

CQRS Revisited

Domain Events

Towards an Event-Centric Architecture

Some Code

What about Testing?

Summary

Page 41: CQRS-Einführung - Teil 2

Testing the Read Model

Thin Read Layer

Tableper

View

Den

orm

ali

zer

Page 42: CQRS-Einführung - Teil 2

Testing the Read Model

Thin Read Layer

Tableper

View

Den

orm

ali

zer

Given that certain Events have occurred

Page 43: CQRS-Einführung - Teil 2

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

Page 44: CQRS-Einführung - Teil 2

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

Page 45: CQRS-Einführung - Teil 2

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

Page 46: CQRS-Einführung - Teil 2

Testing the Write Model

Domain Model

Command Handlers

Page 47: CQRS-Einführung - Teil 2

Testing the Write Model

Domain Model

Command Handlers

Given that certain Events have occurred

Page 48: CQRS-Einführung - Teil 2

Testing the Write Model

Domain Model

Command Handlers

CommandsWhen a specific Commands is sent

Given that certain Events have occurred

Page 49: CQRS-Einführung - Teil 2

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

Page 50: CQRS-Einführung - Teil 2

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

Page 51: CQRS-Einführung - Teil 2

CQRS Revisited

Domain Events

Towards an Event-Centric Architecture

Some Code

What about Testing?

Summary

Page 52: CQRS-Einführung - Teil 2

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

Page 53: CQRS-Einführung - Teil 2

Proven Technology:

Mature business models move away from Update/Delete and become purely transactional:

▫ Human Resources

▫ Medicine

▫ Bookkeeping

▫ …

▫ Banking

▫ Financial Trading

▫ Government

Page 54: CQRS-Einführung - Teil 2

What we haven‘t touched yet:

▫ Messaging

▫ Event Sourcing

▫ Sagas

▫ Eventual Consistency

▫ Integration Scenarios

▫ …

Page 55: CQRS-Einführung - Teil 2

Resources

• http:// cqrs.wordpress.com

• http:// groups.google.com/group/dddcqrs

• http:// distributedpodcast.com

• http:// abdullin.com/cqrs

• http://cqrs.wikidot.com

Page 56: CQRS-Einführung - Teil 2

Vielen Dank!

Dennis Traub@Dtraub

Please rate this talk at:

bit.ly/xyb4tx