对清洁建筑中的单一责任的困惑
我有一个课程从队列接收消息,一旦收到消息,我需要将其上传到云,然后将其发送到另一个服务。在一个班级中必须完成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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码揭示了由3个单个步骤组成的过程。您已经创建了单独的实例来处理这些步骤(例如
uploadcsv
和sendfile
)。您可能缺少的是描述 Process 本身的第四类。因此,您可以创建一个新类,称为requestRefundProcessor
或requestRefundorChestrator
或request> 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
andsendFile
). What you may be missing is a fourth class to describe the process itself. So you could create a new class calledRequestRefundProcessor
orRequestRefundOrchestrator
orRequestRefundFlow
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™