command query separation and how it helps in your daily job
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
Command Query Separation
And what it brings to your day jobDennis Doomen
About Me
• Principal Consultant• 16 years IT experience• C++ origins but since
2001 addicated to C#• Specialties– Architecture– Scrum/XP– ALM
• Speaker• Public initiatives– Silverlight Cookbook– C# Coding Guidelines– Fluent Assertions
• Internet– www.dennisdoomen.net– DZone MVB– @ddoomen
WHAT IS CQS AND WHY SHOULD YOU USE IT?
PROF. BERTRAND MEYER
“…asking a question should not change the
answer…”
myObject .UseRepository("Stuff") .WithTransactionSupport() .Retries(3) .LogWarnings() .LogErrors();
var newObject = myObject .UseRepository("Stuff") .WithTransactionSupport() .Retries(3) .LogWarnings() .LogErrors();
CQS IN PRACTICE
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
SO WHAT’S CQRS THEN?
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.”
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
Query Store
Querying
Query Store
Querying
Scaling OpportunitiesFront-End
Command Side
RelationalDatabase
Query Store
Querying
Query Store
Querying
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
HOW TO BUILD IT?
Approach
1. Decide if you need it2. Reconsider if you need it3. Design your domain using
aggregates4. Add commands5. Add queries
Effective Aggregate Design
Effective Aggregate DesignDesign Small
Aggregates
Reference other
aggregates by identity
Model true invariants in consistency boundaries
Update single aggregate per
transaction
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
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
Questions?
Twitterddoomen
Siteswww.dennisdoomen.netwww.csharpcodingguidelines.comfluentassertions.codeplex.comsilverlightcookbook.codeplex.com
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