mardi 31 août 2021

Single responsability principle on complex process

I always had a question on how to assure the single responsability principle when the process I have to assure is quite complex.

I work with a 3 layers architecture Backend : Controller (my API endpoints) | Service (single responsability functions) | Data (access to the DB)

Let's say I have a process ProcessA that is composed by 4 tasks TasksA1, TasksA2, TasksA3, TasksA4.

If I have an endpoint exposed on my controller layer such as : POSTMethodProcessA

How should be composed my code in order to respect the single responsability principle on my service layer ?

The options I see :

Option 1 (the controller must know the process) :

class MyController {
  exports.processA = functions.https.onRequest(req, res) => {
    myservice.doTaskA1(); // single responsability on task1
    myservice.doTaskA2(); // single responsability on task1
    myservice.doTaskA3(); // single responsability on task1
    myservice.doTaskA4(); // single responsability on task1
  });
}

Option 2 (the service know the process and loose the Single responsability)

class MyController {
  exports.processA = functions.https.onRequest(req, res) => {
    myservice.doProcessA();
  });
}

//inside the service (the doProcessA method must be in charge of multiples tasks and loose the single responsability principle :
class MyService {
  function doProcessA() {
    this.doTasksA1();
    this.doTasksA2();
    this.doTasksA3();
    this.doTasksA4();
  }
}

This question is even more complicated to me if the tasks are composed themselves by multiple jobs : FirstJobA1, SecondJobA1, ThirdJobA1 ...

How those complexities layer should be handled on the code structure to respect the single responsability principle is something that always blocked me. Any insight would be a great help !

Aucun commentaire:

Enregistrer un commentaire