Answer the question
In order to leave comments, you need to log in
Controllers in one line VS smearing business logic in the controller - which is worse?
Controllers in one line:
Answer the question
In order to leave comments, you need to log in
Definitely better to keep the business logic separate.
Separately, not only controllers, but in general from everything, even from the framework (in which case you can painlessly change the framework).
From experience, I can say that if the project is used, developed, it will be necessary to constantly make changes, and it is extremely difficult to do this when the business logic is "smeared" across controllers, even more difficult when requests to the database go directly from the controller, errors are inevitable. There will be many mistakes. And finding errors will not be easy.
It is much easier to cover separately implemented business logic with tests than when it is in controllers.
It is also not uncommon for some actions from the business logic side to be initiated either via HTTP, web sockets, GRPC, via cron, or even called from a background task. And here you will definitely have to take out the business logic from the controller. So it turns out that the logic is "smeared" not only in controllers, but also in other places, then it takes a very long time to deal with all this :)
I like the option when there are models (entities) that simply contain data + a minimum of methods, there is a repository , which only writes / reads data from the database and there are many small services that work with models and repositories + they can also have business logic. These same services are good to call in controllers.
On the other hand, if the project is very small, then it makes sense to save time and not bother much.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question