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.

Leave a Reply