Brian Ritchie's Blog

My ramblings on .NET & other development topics

News



Twitter

Blog Roll

Connect with me

CQRS: Command Query Responsibility Segregation

Jason Gerard and I were talking about document databases the other day, and he said "Have you heard about CQRS?"  This was news to me, so I started doing some research.  Wikipedia, which usually provides good information, said the following "CQRS is simply the creation of two objects where there was previously only one. The separation occurs based upon whether the methods are a command or a query (the same definition that is used by Meyer in Command and Query Separation, a command is any method that mutates state and a query is any method that returns a value)."

Ok, that was clear as mud. Maybe a picture will help.

 

This diagram is from Udi Dahan's excellent presentation on CQRS at the London .NET User Group.  Udi is an amazing presenter & his presentation is well worth the watch.  He is also the guy behind NServiceBus.

Here are the basics as I understand them:

  • In a multi-user environment, data on the screen is always stale. Due to this fact, we don't need a complicated ORM to pull "live" data out of our OLTP database.  Instead, we can use a persistent view model to pre-cache the data needed by the UI.
  • The user interface needs to capture the user's intent, not just their input.  It can then build up commands that are submitted asynchronously to the services layer.  This is a more imperative way of doing things and provides the opportunity to inject business processes without changing the user interface.
  • Validation is performed at both the UI & services layer using a shared component.  This decreases the chance of the command being rejected.  If it does fail, we can notify the user via email or some other out-of-band means.
  • This allows our backend process to have as much time as it needs to perform the business logic & update the database. 

By decoupling the queries and the commands, this pattern provides great scalability & simplifies the individual components.  Of course this isn't the silver-bullet architecture, but it does solve a number of problems I've faced with scalable systems. 

One problem for this pattern seems to be how to handle user input that should be reflected in the view model.  Since the view model is read-only, Udi talks about overlaying the input over the data from the view model.  This may work in some basic scenarios, but would break down in others.  This is probably an area you'll want to evaluate before using this pattern.

Besides watching the presentation you can also read Udi's post: Clarified CQRS.

There is even a .NET framework called Ncqrs that in its words "helps build scalable, extensible and maintainable applications by supporting developers apply the CQRS architectural pattern."  It has support for some interesting technologies: NServiceBus, SQLite, Microsoft Azure Platform, StructureMap, and RavenDB.

Comments

BorErurbtum said:

<a href=www.restorebeautynow.com/.../a>  paraffin, paraffin wax, and petrolatum. Application of mineral oil assurance that you will also benefit from them. There are also skin pretty, nor does it feel comfortable. A simple smile can be painful due leads to skin dryness and aging. In fact, smoking and drinking works lost nutrients that the skin needs to keep itself healthy. Once you have

# December 24, 2012 3:31 PM

immimaArrolow said:

Your location is valueble for me. Thanks!

<a href=caiji325.blog.com/>cheap jordans online</a>

# January 1, 2013 9:09 AM

Kepnuandhep said:

Great Post.thanks for share..a lot more wait ..

<a href=jerseys2015.is-great.org/>Lions Jerseys</a>

# January 14, 2013 9:30 PM

immimaArrolow said:

You created some decent points there. I looked on the web for the concern and found most individuals will go together with together with your webpage.

<a href=airjordanhk.moonfruit.com outlet</a>

# January 19, 2013 3:44 AM

fierrexBish said:

It is difficult to find knowledgeable individuals on this topic, but you sound like you know what you're talking about! Thanks

<a href=http://www.discountmkbag.com>michael kors bag</a>

# January 22, 2013 1:59 PM

fierrexBish said:

Spot on with this write-up, I really think this web site requirements a lot more consideration. I'll probably be again to read far more, thanks for that info.

<a href=http://www.discountmkbag.com>michael kors bags</a>

# January 23, 2013 10:19 AM

Kepnuandhep said:

Hello! I just would like to give an enormous thumbs up for the wonderful information you've here on this post. I is going to be coming back to your blog for a lot more soon.

<a href=cheapnfljerseys168.my-board.org/>NFL Jerseys</a>

# January 25, 2013 5:36 AM

fierrexBish said:

Spot on with this write-up, I truly feel this web-site needs a lot more consideration. I'll quite possibly be once again to read far more, thanks for that information.

<a href=http://www.michaelkorsdiscountbag.com>michael kors bags</a>

# January 25, 2013 11:58 AM

fierrexBish said:

Nice post. I find out some thing extra challenging on distinctive blogs everyday. It'll continually be stimulating to read content from other writers and practice just a little some thing from their store. I'd prefer to make use of some using the content on my blog no matter if you do not mind. Natually I'll provide you with a link on your web blog. Thanks for sharing.

<a href=http://www.coolmichaelkorsbags.com>michael kors bags</a>

# February 3, 2013 8:20 PM

fierrexBish said:

Nice post. I learn some thing even more challenging on diverse blogs everyday. It's going to constantly be stimulating to read content from other writers and practice somewhat something from their store. I'd prefer to make use of some with the content on my weblog no matter if you don't mind. Natually I'll provide you with a link on your web blog. Thanks for sharing.

<a href=http://www.christianlouboutincheaps.com>cheap christian louboutin</a>

# March 10, 2013 7:23 PM

CLIETTEUNSUFF said:

Make sure you take part in a contest for among the most beneficial blogs on the web. I will advise this web page!

<a href="cheapmkbag4saleb.ueuo.com/">michael kors bags outlet</a>

# May 14, 2013 4:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)