DDD / CQRS

Previously... in Software Development

Read / Write

  • Forced into 3rd Normal Form
  • Fast Writes
  • Complex and slow reads

The Light...

Command Query Responsibility Segregation

  • Close friend of Command Query Separation
  • For systems, not just methods

Example: Profiles

DTO Flow

  • User loads a profile from 
  • User updates the URL field
  • User tells server to Update the DTO
  • Server performs validation
  • Server saves the DTO
  • Browser reloads the profile

CQRS Flow

  • User loads a profile via query system
  • User updates the URL
  • UpdatePhotoUrl command sent to command system
  • Handler loads the profile
  • Handler sets the photo URL
  • Handler saves the profile
  • Browser reloads the profile via query system

CQRS: Complete

But let's look at some code anyway

The Simple Profile

Old

DTOs

DDD / CQRS

Light query objects

Commands

Transfer

Old

Get

Update

DDD / CQRS

Get (?)

SetPhotoURL

Verbs

Old

As expected

DDD / CQRS

Double

Code Size

Old

Command handler?

DTO?

DDD / CQRS

Domain Object

Business Rules

Old

Fast Reads

Fast Writes

DDD / CQRS

Fast Reads

Fast Writes

Speed

Winner

DTOs

Complex Profiles

Friends complicate everything

Old

Get

Update

DDD / CQRS

Get

SetPhotoUrl

Befriend

Verbs

Old

Getting bigger...

DDD / CQRS

Smaller growth

Code Size

Old

Still unclear...

DDD / CQRS

Domain Object

Business Rules

Old

Slow validation

Fast Writes

Slow Reads

DDD / CQRS

Fast Validation

Fast Writes

? Reads (hint: Fast)

Speed

Old

Difficult

DDD / CQRS

It Just Works

Scale Out

About them reads...

Eventual Consistency

Tailored for Speed

But it's behind...

ORM?

Reconstruction

Testing!

Commands

Events

Exceptions

Given a profile

  • When its URL is set to a shock site
    • An exception is thrown
  • When its URL is set to something normal
    • PhotoURLChanged event is published
  • When a friend is added
    • FriendAdded event is published
    • When the same friend added again
      • Exception
    • When a different friend added
      • Event published

Internal State

Internal State

There are NO getters!

Behaviour Driven

The Read Model

Concurrency

Partitioning

Next Week(s)

Event Sourcing - Attempt 2

Aggregates

Bounded COntext

?

DDD / CQRS

By xwipeoutx

DDD / CQRS

  • 836