L
L
Lopus2021-10-14 17:46:48
Software design
Lopus, 2021-10-14 17:46:48

How to properly separate code between Controller and Service?

Tell me, what rules should be followed when choosing a place for the location of the code?
Controller - Service - Repository. I got confused in terminology and approaches.

Right now I'm thinking about an example:
api: /api/article/update, input parameters - a field with json data, image1 and image2 files in ArticleController.
My first option is to save the files from the controller by calling StorageService.save(image1), modify the Article model and save via ArticleService.save(article)
My second option is to pass the entire dataset to ArticleService.save(article, image1, image2) and already from there to call StorageService

Or do I not think so at all and it is better to do it differently?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Innokenty Ogoyukin, 2021-10-26
@Ogoyukin

Hello, the main problem is in the rules, there are no rules other than standards. Standards can be adopted by project, company, language, and so on. Therefore, it is necessary to start from subject models, from the assignments of classes, layers. What is a service layer, what is a controller, what should the class be able to do, what inputs does the class require, etc.
As I understand it, you divided it into layers, and both the controller and services can act as a facade for services, you just need to define the validation logic. You can make a separate layer of validation, mapping. I would keep it simple and would call from the controller if there is no reason to call from there. But if the logic becomes more complicated, only then make further decisions.
From your examples, the first option is more obvious, and in the second, some additional parameters are required when saving.
For me, the business logic here is in the possibility of changing the Article, then I think you need:

  • In the Article model, add the parameter "image1, image2"
  • In the controller, "map" the data included in the controller into the entity
  • Model data or data coming from outside will always be accessed

To better immerse yourself in this area, you can read about SOLID, DDD, clean architecture.

D
DollyPapper, 2021-10-31
@DollyPapper

Domain level services are where the business logic of your application is stored (with an anemic model, that is, one that does not have business logic in it), application level services are auxiliary logic for the application, but not related to the domain. Controllers are transport. Their task is to accept, process, call whoever is needed, services from the domain level, application level services, etc. and send back the answer. Logic in them should be at a minimum. According to the logic described by you, ArticleService should not save anything itself, this is the task of IMHO StorageService, and ArticleService should delegate this work to it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question