applying cqrs and event sourcing in .net...

21
Applying CQRS and Event Sourcing in .NET applications Dino Esposito JetBrains dino.esposito@jetbrains.com @despos facebook.com/naa4e

Upload: lamkhuong

Post on 06-May-2018

248 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

Applying CQRS and Event

Sourcing in .NET applications

Dino Esposito

JetBrains

[email protected]

@despos

facebook.com/naa4e

Page 2: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

WARNINGThis is NOT simply a shameless plug

but a truly helpful reference

“I will say that in a number of cases, a page

from this book erased a mass of confusion I'd

acquired from Vaughn Vernon's Implementing

Domain-Driven Design. This was written in a

much more concise, clear, practical manner

than that book.”

—(non anonymous) Amazon reviewer

Page 3: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

http://naa4e.codeplex.com

Page 4: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

IITYWIMWYBMAD?

CQRS

If I Tell You What It Means Will You Buy Me A Drink?

2G2BT AGKWE KUTGW WTHIN?

Page 5: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

One

Comprehensive

Software Model

for the Business

Domain

Page 6: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

Query

Returns data

Doesn’t alter

state

Command

Alter state

Doesn’t

return data

Separation

Responsibility

Bertrand Meyer called it

Command/Query Separation back in the 1980s

Page 7: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

ONE BIG ISSUE LEADING TO CQRS

How to design a single model to address of all

aspects and concerns of a business domain?

How to do that effectively?

Theory

Begin End

Practice

Page 8: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

Relevant data

Relevant tasks

WHERE DOES COMPLEXITY COME FROM?

Page 9: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

Relevant data tables

Relevant tasks

WHERE DOES MORE COMPLEXITY COME FROM?

even more

Page 10: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

Presentation layer

Application layer

Infrastructure layer

Canonical layered architecture

Domain layer

Presentation layer

Application layer

Infrastructure layer

CQRS

Commands Queries

Domain layer

Data

access

+

DTO

Page 11: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

O(C x Q) vs. O(C + Q)

ONE BIG ISSUE LEADING TO CQRS

We faced a lot of complexity in modeling and

we thought it was inherent domain complexity

Page 12: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

BenefitsSimplification of the design

Potential for enhanced scalability

Side effectsEasier to

maintain and evolve stacks

Optimize each stack

separately

ASPECTS OF CQRS

Page 13: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

BUSINESS

DOMAIN

Command

Context #1

Query

Context #1

Command

Context #2

Query

Context #2

Command

Context #3

Query

Context #3

CQRS Design

DDD Analysis

CONTEXT

#1

CONTEXT

#2

CONTEXT

#3

Requirements

Page 14: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

FLAVORS OF CQRS

• TX Script

• Your choice of DAL

• Shared DB

Regular

• Aggregates

• Your choice of DAL

• Data DB + snapshots

Premium

• Event-driven architecture

• LET or any choice of DAL

• Data DB + snapshots + event storeDeluxe

Page 15: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

REGULAR CQRS

PRESENTATION

TX SCRIPT TX SCRIPT

APPLICATION APPLICATION

DAL

Simply use s-procs to

read or ADO.NET or EF

or whatever else suits

you. Just use DTO to

bring data back.

Page 16: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

PREMIUM CQRS

PRESENTATION

DOMAIN LAYER

APPLICATION APPLICATION

DAL

Business logic

implemented through

aggregates and

domain model. State

of aggregates also

persisted in a format

suitable for the UI.

Sync

Page 17: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

DELUXE CQRS

PRESENTATION

EVENT-DRIVEN BIZ

APPLICATION APPLICATION

DAL

Business logic

implemented through

aggregates and

domain model. State

of aggregates also

persisted in a format

suitable for the UI.

Sync

Page 18: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

DELUXE CQRS

APPLICATIONApplication logic

expressed through

commands pushed to

a bus generating

events handled by

sagas (stateful) and

handlers (stateless).

Optionally events

recorded to a log.

Sync

DOMAIN LAYER Event

store

QUERY

STACK

…Saga(s) Handler(s)

BUS

Page 19: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

DEMO

Page 20: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

AT THE VERY END OF THE DAY

CQRS brings just one core idea: keep write and read stacks completely separated and based on different models and even different implementation patterns and technologies.

Best-selling point of CQRS is that it looks like common sense and a smarter way of doing just the same things.

CQRS is not a philosophy or a methodology:

it's just about writing code.

Page 21: Applying CQRS and Event Sourcing in .NET Applicationssddconf.com/brands/sdd/library/Applying_CQRS_Event_Sourcing.pdf · Applying CQRS and Event Sourcing in .NET applications ... but

FOLLOW

Thank You!

facebook.com/naa4e

software2cents.wordpress.com

[email protected]

@despos

http://naa4e.codeplex.com/ Project MERP