Categories: DotNet5

Microservices – Command and Query Responsibility Segregation(CQRS)

Step 4 Create basic CRUD methods; this article samples GET and POST methods. Create the “Queries” folder, then create the GET query class file. Making the POST query class file is under the “Command” folder in another operation.

GET Part:

Fig 6 GetDemoQuery object source code.
Fig 7 GetDemoByIdQuery object source code.

POST Part:

Fig 8 AddDemoCommand object source code.
Fig 9 AddDemoCommand2 object source code.

Step 5 Create the “Handler” folder, then create the GET handler class file of the query model and the POST handler class file of the command model. The GET handler class file inherits the GET query class object by the “IRequestHandler” interface class object. Hence, it adds the generic type interface class constraint variable of the GET handler class in the DemoServices class object.

GET Part:

Fig 10 GetDemoHandler class source code.
Fig 11 GetDemoByIdHandler class source code.

The POST handler class file inherits the POST query class object by the “IRequestHandler” interface class object, constraining the POST handler class, which adds the generic type interface class. Its constraint variable adds to the DemoServices class object.

POST Part:

Fig 12 AddDemoHandler class source code.

Step 6 Create the controller class for the demo CQRS with MediatR. Its controller file adds the IMediator interface class object’s constraint variable, then creates the GET and the POST method.

GET method source code below:

Fig 13 Controller – GET method.

POST method source code below:

Fig 14 Controller – POST method.

Page: 1 2 3

davidsky69

Recent Posts

SOLID Principles – Single-responsibility principle

The single-responsibility principle (SRP) is a computer-programming principle that states that every module, class or…

4 years ago

Microservices – Domain-driven design(DDD)

Domain-driven design (DDD) is a software design approach focusing on modelling software to match a…

4 years ago

Microservices – Onion architecture

A microservice architecture – a variant of the service-oriented architecture (SOA) structural style – arranges…

4 years ago

Unit Of Work pattern concepts in .NET

The Repository pattern and Unit of Work pattern are used together in most time. Unit…

4 years ago

Behavioral Patterns – Visitor

Visitor – It allows the program changes the algorithm and the operations that base on the visitor…

4 years ago

Behavioral Patterns – Template method

Template method – It means the program defines an algorithm skeleton in the superclass, which lets subclasses…

4 years ago