对清洁建筑中的单一责任的困惑

发布于 2025-02-13 12:02:26 字数 1014 浏览 1 评论 0原文

我有一个课程从队列接收消息,一旦收到消息,我需要将其上传到云,然后将其发送到另一个服务。在一个班级中必须完成3个不同的工作,我正在做的是:

private async Task ProcessMessageAsync(ProcessMessageEventArgs args)
{
    try
    {
        //i get the message first 
        var incomingMessage = JsonConvert.DeserializeObject<RequestRefundModel>(args.Message.Body.ToString());
        //need to upload it 
        var sendtoBlobResult = await uploadCsv.ExecuteUseCaseAsync(incomingMessage).ConfigureAwait(false);   
        //prepare to send it to another service             
        SendFileToAggregatorModel sendToAggregator = new();
        sendToAggregator.Metadata = new ResponseCsvRefundModel() { Transactions = incomingMessage.FileBody};
        sendToAggregator.TransactionType = incomingMessage.TransactionType;
        sendToAggregator.URL = sendtoBlobResult.URL;
        await sendFile.ExecuteUseCaseAsync(sendToAggregator);             
    }
    catch (Exception ex)
    {
        ////
    }
}

我是否打破了单一责任的规则?如果是这样,我希望您澄清我缺少修复它的内容吗?

I have a class receives message from the queue, once i get the message i need to upload it to cloud and then send it to another service. 3 different jobs have to be done in a single class, what I'm doing is :

private async Task ProcessMessageAsync(ProcessMessageEventArgs args)
{
    try
    {
        //i get the message first 
        var incomingMessage = JsonConvert.DeserializeObject<RequestRefundModel>(args.Message.Body.ToString());
        //need to upload it 
        var sendtoBlobResult = await uploadCsv.ExecuteUseCaseAsync(incomingMessage).ConfigureAwait(false);   
        //prepare to send it to another service             
        SendFileToAggregatorModel sendToAggregator = new();
        sendToAggregator.Metadata = new ResponseCsvRefundModel() { Transactions = incomingMessage.FileBody};
        sendToAggregator.TransactionType = incomingMessage.TransactionType;
        sendToAggregator.URL = sendtoBlobResult.URL;
        await sendFile.ExecuteUseCaseAsync(sendToAggregator);             
    }
    catch (Exception ex)
    {
        ////
    }
}

am I breaking the rule of single responsibility? If so, I would like you clarify what I'm missing to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2025-02-20 12:02:26

您的代码揭示了由3个单个步骤组成的过程。您已经创建了单独的实例来处理这些步骤(例如uploadcsvsendfile)。您可能缺少的是描述 Process 本身的第四类。因此,您可以创建一个新类,称为requestRefundProcessorrequestRefundorChestratorrequest> requestRefundflow >是描述要求退款所需的各个步骤。我故意使用 May 的语言可以,因为决定是否有意义。仅针对8行代码创建一个新类可能是过分的。再说一次,如果还有其他类可以重复使用此代码,那么这样做是有意义的。我认为,我会将代码移至其自己的类中,因为它有助于从您使用的技术消息处理框架中提取实际业务流程。

最后,单一责任原则问题永远是:这取决于™

Your code reveals a process that consists of 3 individual steps. You have already created separate instances to handle these steps (e.g. uploadCsv and sendFile). What you may be missing is a fourth class to describe the process itself. So you could create a new class called RequestRefundProcessor or RequestRefundOrchestrator or RequestRefundFlow whose sole responsibility is to describe the individual steps required to request a refund. I am purposely using language like may and could, because it is up to you to decide wether this makes sense or not. Creating a new class just for 8 lines of code may be overkill. Then again, if there are other classes that can reuse this code, then it makes sense to do it. In my opinion, I would move the code to its own class, because it helps to extract the actual business process from the technical message processing framework you are using.

In the end, the single responsibility principle is an architectural guideline, so the answer to your question will always be: it depends™

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文