Geeks With Blogs
Liam McLennan

image Command query separation is a strategy, proposed by Bertrand Meyer, that each of an object’s methods should be either a command or a query. A command is an operation that changes the state of a system, and a query is an operation that returns a value.

This is not the same thing as CQRS, hence why I think that CQRS is poorly named.

An Example of Command Query Separation

Consider a system that models books and shelves. There is a rule that a shelf may not be removed if it holds any books. One way to implement the removal is to write a method Shelf.Remove() that internally checks to make sure that the shelf is empty before removing it. If the shelf is not empty then it is not removed and an error is returned. To implement this feature following the principle of command query separation would require two methods, one to query the shelf and determine if it is empty and a second method to remove the shelf. Separating the query from the command makes the shelf class simpler to use because the state change is clear and explicit.

Posted on Sunday, May 30, 2010 7:01 PM | Back to top

Copyright © Liam McLennan | Powered by: