Questions tagged [command-query-separation]

Command–Query Separation (CQS) is a principle of imperative computer programming. Not to be confused with Command-Query Responsibility Segregation (CQRS), a distributed design pattern derived from CQS.

Command–Query Separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.

It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects. It is noteworthy that rigid implementation of this specification makes tracking the number of times queries have been issued essentially impossible; it is clearly intended as a programming guideline rather than a rule for good coding, such as avoiding the use of a goto from a nested loop.

Not to be confused with Command-Query Responsibility Segregation (), a distributed design pattern derived from CQS.

See Wikipedia for more.

52 questions
117
votes
5 answers

Difference between CQRS and CQS

I am learning what is CQRS pattern and came to know there is also CQS pattern. When I tried to search I found lots of diagrams and info on CQRS but didn't found much about CQS. Key point in CQRS pattern In CQRS there is one model to write (command…
Mr punch
  • 1,756
  • 4
  • 16
  • 23
69
votes
9 answers

How would one apply command query separation (CQS), when result data is needed from a command?

In wikipedia's definition of command query separation, it is stated that More formally, methods should return a value only if they are referentially transparent and hence possess no side effects. If I am issuing a command, how should I…
Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
15
votes
5 answers

Event sourcing infrastructure implementation

I implement Event Sourcing and CQRS pattern in my application. I inspired by CQRS journey where I downloaded sample code. There I found whole infrastructure for Event sourcing (CommandHandlers, EventHandlers, Events, Envelopes ... etc.), but it is…
y0j0
  • 3,369
  • 5
  • 31
  • 52
8
votes
3 answers

Abuse of Closures? Violations of various principles? Or ok?

Edit: fixed several syntax and consistency issues to make the code a little more apparent and close to what I actually am doing. I've got some code that looks like this: SomeClass someClass; var finalResult = DoSomething(() => { var result…
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
8
votes
2 answers

Command Query Separation: commands must return void?

If CQS prevents commands from returning status variables, how does one code for commands that may not succeed? Let's say you can't rely on exceptions. It seems like anything that is request/response is a violation of CQS. So it would seem like you…
MKaras
  • 2,063
  • 2
  • 20
  • 35
7
votes
5 answers

Are fluent interfaces a violation of the Command Query Separation Principle?

I started writing a fluent interface and took a look at an older piece Martin Fowler wrote on fluent interfaces (which I didn't realize he and Eric Evans coined the term). In the piece, Martin mentions that setters usually return an instance of the…
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
7
votes
1 answer

After `x = x.y()`, why did `x` become `None` instead of being modified (possibly causing "AttributeError: 'NoneType' object has no attribute")?

If your question was closed as a duplicate of this, it is because you have some code of the general form x = X() # later... x = x.y() # or: x.y().z() where X is some type that provides y and z methods intended to mutate (modify) the object…
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
6
votes
4 answers

Python naming conventions for functions that do modify the object or return a modified copy

What would be the naming conventions in Python for functions that can return a modified object or that just modifies the instance. Let's assume you want to implement both, how you should name the functions? Example: Let's assume that you want a…
sorin
  • 161,544
  • 178
  • 535
  • 806
6
votes
2 answers

Is running a query from a command a violation of Command-Query Separation?

Given a real-world anonymous shopping cart, the "AddToCart" workflow must do the following steps: Lookup the current product from the database. Get the price from the product or use a service to calculate the price on user selections and other…
6
votes
3 answers

Is returning a Task violating the CQS-principle?

The CQS-principle (https://en.wikipedia.org/wiki/Command%E2%80%93query_separation) states that a command should return void. The recommendation for async methods is to never return void (https://msdn.microsoft.com/en-us/magazine/jj991977.aspx),…
Emil Lundin
  • 577
  • 5
  • 14
6
votes
3 answers

Using the Command-Query Separation principle in MVC Controllers

I like the idea of Command Query Separation but can't see how to use it within an MVC Controller action which is adding an entity, and needs the new entity's ID after adding it. For example, in the simplified example below a service is used to…
Appetere
  • 6,003
  • 7
  • 35
  • 46
4
votes
1 answer

Separate application service for command / query in CQRS implementation in Domain Driven Design?

When implementing CQRS with Domain Driven Design, we separate our command interface from our query interface. My understanding is that at the domain level this reduces complexity significantly (especially when using event sourcing) in the domain…
drizzie
  • 3,351
  • 2
  • 27
  • 32
4
votes
2 answers

Should the rule "one transaction per aggregate" be taken into consideration when modeling the domain?

Taking into consideration the domain events pattern and this post , why do people recomend keeping one aggregate per transaction model ? There are good cases when one aggregate could change the state of another one . Even by removing an aggregate…
4
votes
2 answers

WPF Client - Should I make calls to WCF service in background thread?

I have a WPF client that makes calls to 2 WCF services. One service is for querying only and one service is for commands (CQS pattern). How should I make the calls to the commands service ? I read somewhere that all the operations in the command…
John Miner
  • 893
  • 1
  • 15
  • 32
3
votes
3 answers

Implementation of Event-Sourcing / CQRS approach in api-platform

on official Api-Platform website there is a General Design Considerations page. Last but not least, to create Event Sourcing-based systems, a convenient approach is: to persist data in an event store using a custom data persister to create…
1
2 3 4