Command query responsibility segregation (CQRS) generalizes CQS to message-driven and event-driven architectures: it applies the CQS principle by using separate Query and Command messages to retrieve and modify data, respectively.
Wiki
The Command query responsibility segregation(CQRS) is by two elements combination. The first element is Command-query separation(CQS), an imperative computer programming principle from developing the Eiffel programming language by Bertrand Meyer. Every method should execute the command task or the query task with data. In other words, asking a question should not change the answer. The second element is the event-driven architecture (EDA), a software architecture paradigm promoting the production, detection, consumption of, and reaction to events.
How to implement the CQRS in the microservices of the .NET 5 web API?
The many blog title microservices source code sample cases or the enterprise’s architect advice will give the clients the microservice’s CQRS solution, which is made by the MediatR Nuget packages. The MediatR Nuget package is a lazy solution, but it is still suitable for beginner-level applications. The MediatR is a kind of design pattern which is a mediator pattern. The developers can develop the mediator pattern in the microservices or use off-the-shelf tools to implement it.
Note: If you are a junior developer or your team is a junior developer team, which means your team is a favorite using the third-party tools or the suit software tools, the author will give you/your team advice that is “Don’t do things you’re not good at. Do the things with familiar things and do the things you are good at.“
The CQRS of the microservice by graphic explained:
Some service objects send messages to the mediator node point, and the mediator node point then invokes multiple services to handle the messages. There is no direct dependency between any of the components, which reason why the inversion of control is worthwhile and enables loose coupling.
Step 1 Install the MediatR NuGet package.
PM> install-package MediatR PM> install-package MediatR.Extensions.Microsoft.DependencyInjection Step 2 Create two interface class files, such as the ISender object and the IPublisher object, which are under the “Notify” folder. Create the IMediator interface object file, which is under the “Notify” folder, then inherits two interface object, such as ISender object and the IPublisher object.
Step 3 Register the MediatR in a Startup.cs file.
services.AddMediatR(typeof(Startup)); What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…