【发布时间】:2019-11-20 05:45:55
【问题描述】:
我在应用程序中使用 MediatR 和 CQRS。以下两条语句存在于多个模块中,可能会在应用程序(它是一个 blazor 应用程序)中同时调用。
await Mediator.Send(new AddIdCommand { Id = id });
和
await Mediator.Send(new DeleteIdCommand { Id = id });
以下是更新同一文本文件的命令。
public class AddIdCommand : IRequest
{
public int Id { get; set; }
public class AddIdCommandHandler : IRequestHandler<DeleteIdCommand>
{
public async Task<Unit> Handle(AddIdCommand request, CancellationToken cancellationToken)
{
// .... update the text file with the Id deleted
}
}
}
public class DeleteIdCommand : IRequest
{
public int Id { get; set; }
public class DeleteIdCommandHandler : IRequestHandler<DeleteIdCommand>
{
public async Task<Unit> Handle(DeleteIdCommand request, CancellationToken cancellationToken)
{
// .... update the text file with the Id added
}
}
}
....
protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService<IMediator>();
两个命令中的Handle(...) 会一直按顺序调用吗? (因此无需担心多个进程更新同一个文件的问题。)
【问题讨论】:
-
请注意 MediatR,但
await通常表示等待,除非当然下面有一些时髦的事情发生 -
这是一个 blazor 应用程序。他们可能是多个用户同时使用该网站。此外,语句在地方被调用。