.NET Core development actual combat (34th Lesson: MediatR: Easy mode command query separation of duties (CQRS)) - Study Notes (under)

34 | MediatR: Easy mode command query separation of duties (CQRS)

In fact, we in the definition of my query time, that can be defined, for example, we define a MyOrderQuery, the names of all orders are output out

namespace GeekTime.API.Application.Queries
{
    public class MyOrderQuery : IRequest<List<string>>
    {
        public MyOrderQuery(string userName) => UserName = userName;

        public string UserName { get; private set; }
    }
}

We then define a query, a query can be here from various places and then return to our data out

namespace GeekTime.API.Application.Queries
{
    public class MyOrderQueryHandler : IRequestHandler<MyOrderQuery, List<string>>
    {
        public Task<List<string>> Handle(MyOrderQuery request, CancellationToken cancellationToken)
        {
            return Task.FromResult(new List<string>());
        }
    }
}

In fact, we like this completes the definition of queries and query processing

We can define Controller

[HttpGet]
public async Task<List<string>> QueryOrder([FromBody]MyOrderQuery myOrderQuery)
{
    return await _mediator.Send(myOrderQuery);
}

This completes the separation of the query and query processing logic

We execute the command is the same implementation, we like this done, then you can input and process definition to our queries in a directory, you can also execute definition and command of our command in a directory, so that our Controller focus on infrastructure authentication or caching, and so some processing logic, it is no longer concerned that my business logic is how look like

Creative Commons License

This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing.

Welcome to reprint, use, repost, but be sure to keep the article signed by Zheng Ziming (containing links: http://www.cnblogs.com/MingsonZheng/ ), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification .

If you have any questions, please contact me ([email protected]).

Guess you like

Origin www.cnblogs.com/MingsonZheng/p/12596100.html
Recommended