introduction to cqrs

63
INTRODUCTION CQRS PIETER JOOST VAN DE SANDE

Upload: pieter-joost-van-de-sande

Post on 14-May-2015

1.713 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to CQRS

INTRODUCTION

CQRS

PIETER JOOST VAN DE SANDE

Page 2: Introduction to CQRS
Page 3: Introduction to CQRS

THANK YOU VSOFT!!

Page 4: Introduction to CQRS

AGENDA• How we do things now

• What CQRS changes to that

• Building a CQRS architecture

• Some demo stuff

• Discussion

• Beer!

Page 5: Introduction to CQRS

TYPICALARCHITECTURES

YOU SHOULD RECOGNIZE THIS

Page 6: Introduction to CQRS

TYPICAL ARCHITECTURES

Page 7: Introduction to CQRS

TYPICAL ARCHITECTURES

Presentation

Domain

Data

Page 8: Introduction to CQRS

N-LAYER• Flexible – we could replace a layer without breaking much.

• Simple separation that is easy to explain.

• Higher abstraction with each layer.

• Known by a lot of people.

• Supported by tools and frameworks

• Lots of resources available

Page 9: Introduction to CQRS

WHAT THE N-TIER TUNNEL BROUGHT US

Page 10: Introduction to CQRS

TYPICAL ARCHITECTURES

Page 11: Introduction to CQRS

TWO FUNDAMENTAL THINGS

EVERY SYSTEM HAS THIS

Page 12: Introduction to CQRS

DATA

Page 13: Introduction to CQRS

TRANSITIONS

Page 14: Introduction to CQRS

WHAT IS CQRS?

COMMAND

QUERY

RESPONSIBILITY

SEGREGATION

Page 15: Introduction to CQRS

CQRS CONCEPT

MOVE FROM N-LAYER TO…

Page 16: Introduction to CQRS

N-LAYERARCHITECTURE

Presentation

Domain

Data

Page 17: Introduction to CQRS

Presentation

Domain

Data

Page 18: Introduction to CQRS

Presentation

DomainData

Page 19: Introduction to CQRS

Presentation

DomainData

Page 20: Introduction to CQRS

Read State transitions

Presentation

DomainData

Page 21: Introduction to CQRS

FROMCQRS CONCEPTTOARCHITECTURE

THIS ADDS MORE VALUE!

Page 22: Introduction to CQRS
Page 23: Introduction to CQRS

User interface

Page 24: Introduction to CQRS

DEMAND FOR DATA

BECAUSE WE HAVE SOME SCREENS TO FILL

Page 25: Introduction to CQRS

WE READ A LOT

Page 26: Introduction to CQRS

WE HAVE MULTIPLE SCREENS TO FILL

Page 27: Introduction to CQRS

DATA SHOULD BE CLOSE TO US

Page 28: Introduction to CQRS

DEMAND FOR DATA• User interface has different views to fill.

• It should not have to execute business rules on the data.

• It requests data many times.

• It should be close to us.

• Getting data should be simple (select * from x where y)

Examples:

• Get all products of user X• Get total price of all orders of month May

Page 29: Introduction to CQRS

User interface

Page 30: Introduction to CQRS

User interface

Read side

Page 31: Introduction to CQRS

Read side

User interface

Read databases

Page 32: Introduction to CQRS

Read side

User interface

Read databases

query

Page 33: Introduction to CQRS

READ SIDE

The only need it has to serve is reading.

• Do we need an relational data here?

• Contains the data in an form we need

• Facades should be minimized

• Read models do not have to be on one location...

• They should be close to the client

Page 34: Introduction to CQRS

Read side

User interface

Read databases

query

Page 35: Introduction to CQRS

DEMAND FOR CHANGE

OTHERWISE THINGS ARE STATIC

Page 36: Introduction to CQRS

I WANT TO TELL THE SYSTEM SOMETHING

Please move customer X to this new address

Page 37: Introduction to CQRS

CHANGE HAS INTENT

Users don’t make changes for nothing.

This intent has value!

Correct the address for user X

Customer X moved to new addressVS.

Page 38: Introduction to CQRS

MULTIPLE STEPS

Users can take multiple steps before they reach the end goal. These steps can be very useful to us.

Create Cart Add item Add item Add item Remove

item Purchase

Page 39: Introduction to CQRS

COMMANDS• Model that is optimized the describe changes.

• Commands contain all the data needed to execute the underlying action.

• Contain the intent of the change.

Examples:

• AddProductToShoppingCart• PurchaseOrder• MoveUserToNewAddress• CorrectAddressForUser

Page 40: Introduction to CQRS

Read side

User interface

Read databases

query

Page 41: Introduction to CQRS

Write side Read side

User interface

Read databases

query

Page 42: Introduction to CQRS

Commandhandlers

commands

Write side Read side

User interface

Read databases

query

Page 43: Introduction to CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ain

Write side

Facade

Page 44: Introduction to CQRS

GREG YOUNG:

“State transitions are an important part of our problem space and should be modeled within our domain.”

Page 45: Introduction to CQRS

THE ‘OLD’ DOMAIN MODEL

Page 46: Introduction to CQRS

DOMAIN FOCUSED ON BEHAVIOR

Page 47: Introduction to CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ain

Write side

Facade

Page 48: Introduction to CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ain

Write side

Repository

Facade

Page 49: Introduction to CQRS

User interface

Commandhandlers

Read databases

Read side

commands DTO’s

Dom

ain

Write side

Repository

Facade

?

Page 50: Introduction to CQRS

EVENTS

All changes are captured by events.

• The read models keep themself up to date from it.

• Other systems use it for intergration.

Examples

• ProductAddedToShoppingCart• OrderPurchased• UserMovedToNewAddress• AddressCorrectedForUser

Page 51: Introduction to CQRS

EVENTUAL CONSISTENCY• Updates will eventually propagate through the system.

• This means we need to think differently about concurrency!

Page 52: Introduction to CQRS

User interface

Commandhandlers

RepositoryRead databases

Write side Read side

eventscommands DTO’s

Dom

ain

Facade

Page 53: Introduction to CQRS

User interface

Commandhandlers

Repository

Den

orm

alize

rs

Read databases

Write side Read side

eventscommands DTO’s

Dom

ain

Facade

Page 54: Introduction to CQRS

DENORMALIZERS

• Will keep the read models up to date based on the event stream.

• An event can cause multiple updates.

Page 55: Introduction to CQRS

User interface

Commandhandlers

Repository

Event bus

Den

orm

alize

rs

Read databases

Write side Read side

events

events events

commands DTO’s

Dom

ain

Facade

Page 56: Introduction to CQRS

EVENT SOURCING

Page 57: Introduction to CQRS

User interface

Commandhandlers

Repository

Event bus

Den

orm

alize

rs

Read databases

Write side Read side

events

events events

commands DTO’s

Dom

ain

Facade

events

Page 58: Introduction to CQRS

EVENT STORE

• Contains all events

• Contains the audit log

• Read models can re-build themself of it

• The domain uses it to build the current state

Page 59: Introduction to CQRS

FINAL FLOW

Page 60: Introduction to CQRS

CQRS WRAPUP• The domain receives commands and publishes domain

events.

• All state changes are represented by domain events

• The read models are updated as a result of the published Events

• All Queries go directly to the read models, the domain model is not involved

Page 61: Introduction to CQRS

BENEFITS• Fully encapsulated domain that only exposes behavior

• Keep users intent

• Bullet-proof auditing and historical tracing

• No object-relational impedance mismatch

• Optimized and multiple read models

• Testability

• Easy integration with external systems

Page 62: Introduction to CQRS

NCQRS

THE CQRS FRAMEWORK FOR .NET

Page 63: Introduction to CQRS

THANK YOU!YOU’VE BEEN A GREAT AUDIENCE!