Separation of responsibilities for implementation

Asked

Viewed 49 times

1

I would like to ask a question about the separation of responsibilities in an application. I created an API on Node to practice the concepts and I’m refactoring the code to suit best practices, so I have the following structure:

Route

routes.post('/transactions/create', createTransaction);

Controller

const createTransaction = async (request, response) => {
    const { date, category, description, value, type } = request.body;
    const day = date.slice(8);
    const month = date.slice(5, -3);
    const year = date.slice(0, -6);
    const yearMonth = year + "-" + month;
    const yearMonthDay = year + "-" + month + "-" + day;

    try {
        const newTransaction = await transactionsRepository.createTransaction(description, category, value, day, month,
        year, yearMonth, yearMonthDay, type);
        response.status(200).send(newTransaction);
    } catch (e) {
        response.status(500).send( { message: 'Failed to load transaction'} );
    }
}

Repository

const createTransaction = async (description, category, value, day, month, year, yearMonth, yearMonthDay, type) => {
    const newTransaction = await transactionsModel.insertMany({
      description,
      value,
      category,
      year,
      month,
      day,
      yearMonth,
      yearMonthDay,
      type,
    });

    return newTransaction;
};

As you can see in the controller, it currently receives a date and treats it to be sent to the bank. I read that there should be a service layer between the controller and the repository for business rules.

My question is, should this date treatment be on the service layer or should it be in a package utils or helpers? I’m doubtful what the service layer should contain.

  • 2

    if you have a "business" layer you can treat business rules there. Always think about the context of "what purpose" of what you want to do. Let’s take the example of the date, imagine that the date needs to have a "yyyy-mm-dd" format to be displayed. If it is a "display" rule it should be done in that layer. Now imagine that it is a data model rule, it should be done there (Repository), and so on. The goal of the layers is to isolate responsibilities is to maintain this part on its own and test it on its own, so look at the "rule" purpose to see which layer it looks best on

  • If you have a single rule, you can create a class for that, a helper something like that. When creating a layer, you need to think about interfaces, dependency injection, testing... always create what you really need, if you don’t have many rules you will end up creating a layer that won’t do what would be your purpose

1 answer

1


If the date handling complies with a business specification and was requested by business experts, then it should be in a service class within its domain model to be consumed by the controller.

Now, if the processing only exists to tailor the date to the database specifications, then it could be on a helper within the persistence layer, being used by the repositories that need it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.