在CQRS架构中生成QR代码时使用哪种方法?

huangapple go评论56阅读模式
英文:

What approach to use when generating qr code in CQRS architecture?

问题

I need to generate a qr code in my web api and save the data encrypted in the qr code to the database.
我需要在我的 Web API 中生成一个 QR 码,并将 QR 码中的数据加密后保存到数据库中。

I have an action method in my controller GenerateQrCode which should generate a qr code through an external service, save the qr code data to the database, and return json with the generated qr in base64 to the front, the question is, how can I generate a qr code in one request, save it in db and get response as json using cqrs architecture, split generation into command and request? and in the action method to call the command first and then the request to receive? Or should there be two different action methods to be used in the same operation on the front in succession? As far as I know, cqrs assumes that the command does not return a result.
我在我的控制器 GenerateQrCode 中有一个动作方法,它应该通过外部服务生成一个 QR 码,将 QR 码数据保存到数据库中,并将生成的 QR 码以 base64 的形式返回给前端的 JSON。问题是,如何在一个请求中生成 QR 码,保存到数据库并以 JSON 形式获得响应,使用 cqrs 架构拆分生成成命令和请求?在动作方法中先调用命令然后再接收请求吗?还是应该有两个不同的动作方法,在前端的同一操作中连续使用?据我所知,cqrs 假定命令不返回结果。

My action method:
我的动作方法:

app.MapGet("/generateQrCode", async ([FromServices] IPublishEndpoint publishEndpoint, [FromServices] IMediator mediator) =>
{
var result = await mediator.Send(new GetGeneratedQrCodesQuery());
return result;
});

My QueryHandler:
我的查询处理程序:

internal class GetGeneratedQrCodesQueryHandler : IRequestHandler<GetGeneratedQrCodesQuery, string>
{
private readonly IQrCodeClient _qrCodeGenerator;
public GetGeneratedQrCodesQueryHandler(IQrCodeClient qrCodeGenerator) => _qrCodeGenerator = qrCodeGenerator;
public async Task Handle(GetGeneratedQrCodesQuery request, CancellationToken cancellationToken)
{
//Here i want to save qr code data to db...
return await _qrCodeGenerator.GetQrCode(request.ReceiverData.ToString(), cancellationToken);
}
}

On the front side of the system, a button is supposed to be pressed by pressing which a request is sent to the generateQrCode method, a qr is generated, the data is stored in the database, and the qr is returned as a json field in base64.
在系统的前端,应该按下一个按钮,然后发送请求到 generateQrCode 方法,生成一个 QR 码,将数据存储到数据库中,并将 QR 码以 base64 形式返回为 JSON 字段。

I will be most grateful for your help!
我将非常感谢您的帮助!

英文:

I need to generate a qr code in my web api and save the data encrypted in the qr code to the database.
I have an action method in my controller GenerateQrCode which should generate a qr code through an external service, save the qr code data to the database, and return json with the generated qr in base64 to the front, the question is, how can I generate a qr code in one request, save it in db and get response as json using cqrs architecture, split generation into command and request? and in the action method to call the command first and then the request to receive? Or should there be two different action methods to be used in the same operation on the front in succession? As far as I know, cqrs assumes that the command does not return a result.

My action method:

app.MapGet(&quot;/generateQrCode&quot;, async ([FromServices] IPublishEndpoint publishEndpoint, [FromServices] IMediator mediator) =&gt;
{
    var result = await mediator.Send(new GetGeneratedQrCodesQuery());
    return result;
});

My QueryHandler:

internal class GetGeneratedQrCodesQueryHandler : IRequestHandler&lt;GetGeneratedQrCodesQuery, string&gt;
{
    private readonly IQrCodeClient&lt;string&gt; _qrCodeGenerator;
    public GetGeneratedQrCodesQueryHandler(IQrCodeClient&lt;string&gt; qrCodeGenerator) =&gt; _qrCodeGenerator = qrCodeGenerator;
    public async Task&lt;string&gt; Handle(GetGeneratedQrCodesQuery request, CancellationToken cancellationToken)
    {
        //Here i want to save qr code data to db...
        return await _qrCodeGenerator.GetQrCode(request.ReceiverData.ToString(), cancellationToken);
    }
}

On the front side of the system, a button is supposed to be pressed by pressing which a request is sent to the generateQrCode method, a qr is generated, the data is stored in the database, and the qr is returned as a json field in base64.

I will be most grateful for your help!

答案1

得分: 1

First off, CQRS isn't an architecture, it's a principle and nothing of your use case is a case for DDD.

Second, your naming is a mess.

You call your message GetGeneratedQrCodesQuery, but in reality it's not retrieving it (query), but generating it (_qrCodeGenerator.GetQrCode(...)).
As such, the proper naming would be GenerateQrCode (generate and persist the QR Code) and after it's generated (and persisted), you would retrieve it with a query GetQrCode/RetrieveQrCode.

This can be in the same action or separate. Since MediatR isn't an async messaging library but an in-process synchronous library, you know that after awaiting the first command, the QR Code will be persisted, otherwise, it would throw an exception, which allows you to call the query right after.

英文:

First off, CQRS isn't an architecture, it's a principle and nothing of your use case is a case for DDD.

Second, your naming is a mess.

You call your message GetGeneratedQrCodesQuery, but in reality it's not retrieving it (query), but generating it ( _qrCodeGenerator.GetQrCode(...)).
As such, the proper naming would be GenerateQrCode (generate and persists teh QR Code) and after it's generated (and persisted), you would retrieve it with a query GetQrCode/RetrieveQrCode.

This can be in the same action or separate. Since MediatR isn't an async messaging library but a in-process synchronous library, you know that after awaiting the first command, the QR Code will be persisted, otherwise it would throw an exception, which allows you to call the query right after.

huangapple
  • 本文由 发表于 2023年5月21日 04:21:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定