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
3
votes
3 answers

Single function classes (named as verbs)

I'm trying out a new code structure where I'm splitting all my giant repositories and factories into loads of smaller classes each with a single responsibility. And on top of this I'm using verbs for class names as I think that it's the best way to…
svejk
  • 63
  • 4
3
votes
2 answers

Why does the CQS principle require a Command not to return any value?

The CQS principle says every method should either be a command that performs an action, or a query that returns data to the caller, but not both. It makes sense for a Query not to do anything else, because you don't expect a query to change the…
Daniel B
  • 4,145
  • 1
  • 21
  • 21
3
votes
3 answers

Violation of Command-Query Separation while creating new database entry

I have a method that creates some entry in database and returnes it's Id. I would like to somehow separate these two because it violates Command-Query Separation. e.g. method (simplified): int CreatePost(database::Post newPost) { using(var db =…
Miro
  • 1,778
  • 6
  • 24
  • 42
3
votes
1 answer

Exceptions to the "Command-Query Separation" Rule?

Command-Query Separation "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." a = [1, 2, 3] last =…
RyanScottLewis
  • 13,396
  • 16
  • 56
  • 84
2
votes
1 answer

CQRS and unit of work - one command for all the changes, or many commands + a save command?

In a screen that displays many order detail lines of order, users can add, update, or delete rows or columns and then click the "Save" button to save all the order changes in one transaction. Option 1: Create one Command (which will be created by…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
2
votes
2 answers

How to update read model in CQRS pattern on schema change of write model database?

While using CQRS pattern with domain events to generate read model. If we add new column in database with some default data or insert new row directly from sql query manually, in that case no events are generated so how to update the existing…
2
votes
1 answer

How to generate read data in CQRS from existing write database?

Currently we have an Asp.net application which uses SQL server for write and also read queries as a normal monolith application. Now we want to move to CQRS. In CQRS the read model is generated on basis of events. But for the previous transactional…
2
votes
2 answers

What are the recomendation for developing .net core 2.2 web api for following bulleted points?

I am developing a new WebApi using .NetCore2.2, Autofac4, Dapper. There are few very basic questions because this is my first WebApi project. As part of this project I have to write both unit-test and integration-test. My questions are as follows…
2
votes
1 answer

CQS and updating an existing entity

I'm just trying to get my head around how one goes about updating an entity using CQS. Say the UI allows a user to update several properties of a particular entity, and on submit, in the back-end, an update command is created and dispatched. The…
CraigM
  • 561
  • 8
  • 20
2
votes
1 answer

Maintaining CQS When Tracking Number Of Queries

In my web app I'm tracking view counts on pages. Right now, the action in the controller issues a command to the data layer to increment the view count on the model before returning the result of the query. This action seems to break the rules of…
glcheetham
  • 973
  • 8
  • 23
2
votes
2 answers

How to identify if a project has used CQS OR CQRS? What is the difference between CQS and CQRS?

I might sound dumb with this question but i am really confused. Does creating a command,query,commandhanlder,queryhandler and repositories and using dependency injection to resolve queryhandlers and commandhandlers based on query and command…
2
votes
1 answer

Communicating Concurrency Conflicts to the Application Layer

When communicating concurrency conflicts to your application layer, is there an alternative to using exceptions that also respects the principle of Command-Query Separation, or are exceptions the best mechanism we have (in languages that support…
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
2
votes
1 answer

How to Read and Write using CQS

I'm going to start off a new project with CQS (as one aspect of its design), but NO CQRS + Event Sourcing, or Event Streaming, or Historical Modeling. When I come across a situation in which I will have a large set of people using a small set of…
Aaron
  • 3,068
  • 2
  • 21
  • 44
2
votes
3 answers

Command/Query separation when using an ORM

Within my various projects I implement the command/query separation pattern and use NHibernate as my ORM. In general I keep my commands and queries in separate projects relevant to the particular set of activities such as UserManagement,…
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
1
vote
2 answers

Is there any rule in CQRS which doesn't allow to use a query and a command in the same controller action?

For example: I want to delete an item or return 404 if it doesn't exist from a controller action. Am I breaking any rules? Commands are still separated from queries. [ApiController] public class PostsController : ControllerBase { …
Pan Zdrowie
  • 327
  • 1
  • 4
  • 13