Implementing the CQRS design pattern
In this section, we will look at the Command Query Responsibility Separation (CQRS) design pattern. In simple terms, a command is a method that performs an action, while a query is a method that returns data. Commands do not perform queries, and queries do not perform commands. Commands can have separate models for queries. Now, let’s write a simple console application that demonstrates how easy it is to implement this pattern, which is used extensively in microservice development:
- Start a new console application called
CH13_CQRSPattern
. - Add a new class called
CQRSBasedClass
. - Add the
SleepCommand
method:public void SleepCommand(int milliseconds) { Thread.Sleep(milliseconds); }
Our SleepCommand
method is an example of a command. It takes in a parameter that is several milliseconds in length. A command is then executed that causes the current thread to sleep for the number of milliseconds specified...