command query separation and how it helps in your daily job

19
Command Query Separation And what it brings to your day job Dennis Doomen

Upload: dennis-doomen

Post on 14-May-2015

13.522 views

Category:

Technology


3 download

DESCRIPTION

The CQRS architecture has been very hot in the past two years. But by now, I think that the majority of the profession agrees that going all the way is only worthwhile for systems with some very specific requirements. Yet, I believe that the separation of reads and writes in an enterprise system has some interesting advantages. In the past year I have applied this principle to an ASP.NET WebForms project that had nothing to do with web services at all, but gave me a lot of benefits. Even my own initiative, the Silverlight Cookbook, is now based on that. I'd like to get a chance to explain you why I use this technique, why it has become part of my default reference architecture (regardless of the technology), and I'll share some of the advantages and disadvantages of that choice.

TRANSCRIPT

Page 1: Command Query Separation And How It Helps In Your Daily Job

Command Query Separation

And what it brings to your day jobDennis Doomen

Page 3: Command Query Separation And How It Helps In Your Daily Job

WHAT IS CQS AND WHY SHOULD YOU USE IT?

Page 4: Command Query Separation And How It Helps In Your Daily Job

PROF. BERTRAND MEYER

“…asking a question should not change the

answer…”

Page 5: Command Query Separation And How It Helps In Your Daily Job

myObject .UseRepository("Stuff") .WithTransactionSupport() .Retries(3) .LogWarnings() .LogErrors();

var newObject = myObject .UseRepository("Stuff") .WithTransactionSupport() .Retries(3) .LogWarnings() .LogErrors();

CQS IN PRACTICE

Page 6: Command Query Separation And How It Helps In Your Daily Job

void Push(T t);T Pop();bool IsEmpty { get; }

public interface IStack<T> : IEnumerable<T> { IStack<T> Push(T value); IStack<T> Pop(); T Peek(); bool IsEmpty { get; } }

AND ANOTHER EXAMPLE

Page 7: Command Query Separation And How It Helps In Your Daily Job

SO WHAT’S CQRS THEN?

Page 8: Command Query Separation And How It Helps In Your Daily Job

CLEMENS VASTERS

“CQRS is a simple pattern that strictly segregates the responsibility of handling

command input into an autonomous system from the responsibility of handling side-effect-free query/read access on the

same system.”

Page 9: Command Query Separation And How It Helps In Your Daily Job

CQRS = CQS on Architecture Level

Command Service

Front-End

Commands

Domain Model

Repositories

RelationalDatabase

Command Handlers

Query Store

Query Service

Data Access Layer

Projections

Queries Business Actions

Changes

Query Store

No O/R conversions

Sustains the user

intent

Optimized for

consistency

Optimized for querying

Synchronous or asynchronous

Page 10: Command Query Separation And How It Helps In Your Daily Job

Query Store

Querying

Query Store

Querying

Scaling OpportunitiesFront-End

Command Side

RelationalDatabase

Query Store

Querying

Query Store

Querying

Page 11: Command Query Separation And How It Helps In Your Daily Job

Myths of CQRS

• It is an architecture (style)• It should be used by default• It requires Event Sourcing• It requires eventual consistency• It requires a

bus/queues/asynchrony• Commands are fire-and-forget• Solves all concurrency problems• It is easy

Page 12: Command Query Separation And How It Helps In Your Daily Job

HOW TO BUILD IT?

Page 13: Command Query Separation And How It Helps In Your Daily Job

Approach

1. Decide if you need it2. Reconsider if you need it3. Design your domain using

aggregates4. Add commands5. Add queries

Page 14: Command Query Separation And How It Helps In Your Daily Job

Effective Aggregate Design

Page 15: Command Query Separation And How It Helps In Your Daily Job

Effective Aggregate DesignDesign Small

Aggregates

Reference other

aggregates by identity

Model true invariants in consistency boundaries

Update single aggregate per

transaction

Page 16: Command Query Separation And How It Helps In Your Daily Job

How it worksFront-End

App

Command Service

Query Processor

Registry

Command Handler

Aggregate Root

Unit-of-Work

Domain Event

HandlerSimple Data Access Layer

Query Handler

Read DB Write DB

Send commandExecute query

Find command handler

CreatesCreates

Loads

Find by ID and/or version

Send domain event

Find query handler

LINQ, HQL, SQL

Store denormalizeddata Store normalized

data

Invoke method

Page 17: Command Query Separation And How It Helps In Your Daily Job

What to use?Front-End

App

Command Service

Query Processor

Registry

Command Handler

Aggregate Root

Unit-of-Work

Domain Event

HandlerSimple Data Access Layer

Query Handler

Read DB Write DB

WebAPI, WCF, POCO

Autofac, Unity,

StructureMap

NHibernate EF, Dapper

Udi Dahan’s Domain

Event

SQL, Oracle, RavenDB,

NoSQL

SQL, Oracle, RavenDB,

NoSQL

WCF, POCO

Page 18: Command Query Separation And How It Helps In Your Daily Job

Questions?

[email protected]

Twitterddoomen

Siteswww.dennisdoomen.netwww.csharpcodingguidelines.comfluentassertions.codeplex.comsilverlightcookbook.codeplex.com

Page 19: Command Query Separation And How It Helps In Your Daily Job

Reading Material

• Effective Aggregate Designhttp://dddcommunity.org/library/vernon_2011

• Meanwhile…on the command side of my architecturehttp://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91

• Meanwhile…on the query side of my architecturehttp://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=92

• Busting some CQRS mythshttp://lostechies.com/jimmybogard/2012/08/22/busting-some-cqrs-myths/

• Free MSDN eBook: Exploring CQRS and Event Sourcinghttp://www.microsoft.com/en-us/download/details.aspx?id=34774