Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

confusion over single responsibility in clean architecture

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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™

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading