Applying CQRS and Event Sourcing in .NET...

Post on 06-May-2018

238 views 2 download

transcript

Applying CQRS and Event

Sourcing in .NET applications

Dino Esposito

JetBrains

dino.esposito@jetbrains.com

@despos

facebook.com/naa4e

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

http://naa4e.codeplex.com

IITYWIMWYBMAD?

CQRS

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

2G2BT AGKWE KUTGW WTHIN?

One

Comprehensive

Software Model

for the Business

Domain

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

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

Relevant data

Relevant tasks

WHERE DOES COMPLEXITY COME FROM?

Relevant data tables

Relevant tasks

WHERE DOES MORE COMPLEXITY COME FROM?

even more

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

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

BenefitsSimplification of the design

Potential for enhanced scalability

Side effectsEasier to

maintain and evolve stacks

Optimize each stack

separately

ASPECTS OF CQRS

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

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

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.

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

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

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

DEMO

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.

FOLLOW

Thank You!

facebook.com/naa4e

software2cents.wordpress.com

dino.esposito@jetbrains.com

@despos

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