Home > Software design >  confusion over single responsibility in clean architecture
confusion over single responsibility in clean architecture

Time:07-07

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?

CodePudding user response:

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™

  • Related