Where the MVC standard business rule should be?
If there really is business logic, and especially if it is complex, it should stay in the Model.
The concern that the creation of Pattern MVC (back in the 1970s) was trying to resolve is the separation between presentation and business logic, and the controller most often is quite attached with the presentation.
See this Fowler drawing representing the MVC:
Note that View knows Controller, which also knows View; and note that Model knows no one.
In your book Patterns of Enterprise Application Architecture, Fowler says:
"The separation between presentation and model is one of the most fundamental heuristics of good software design."
In this same book, he mentions that the separation between Controller and View may not be so evident in many designs, and that this is not a problem, it is natural that they get confused.
And they get really confused: notice how the Controller actually needs to know the View, he knows the presentation rules (even he is the one who defines these rules).
This intimacy between Controller and View is already an indication that Controller is not good company for business logic (tell me who you’re with and I’ll tell you who you are) if you really want to separate these concepts.
But what is after all "logic" or "rule" of business?
Stackoverflow needs to show the answers first with more votes. Is that a business rule? It certainly is, this is a core principle of the tool.
Stackoverflow can’t admit to an untitled question. That’s also a business rule.
So these rules need to be in Model? They don’t need to be and they probably aren’t. These are very simple business rules, which can be solved in the presentation.
The OS, such an important tool with so many users, may not even have a domain layer concentrating its business rules with high abstraction. It is possible that Models here are only Dtos representing in the system the database data.
The OS is probably an example of a system that classifies itself as low complexity of business rules, where an abstraction of the domain in a layer can be unnecessary and even harmful.
Most of the projects I’ve participated in to date, however, had complex enough business rules and sufficient need for reuse that justified a more elaborate design approach. But surely not all systems justify such an approach! I am not going to say "majority" in general because I have no statistical data. I can only speak from my own context.
If you decide, my son: the rules may or may not stay in the Controller?
They can if the system has simple rules, basically presentation rules.
They should not be if the system has complex rules, a complex domain, and a high need for re-use (the rules need to be available to more than one type of consumer - as end user, service facade, other business services of the system itself, integrations, code bases of other systems, etc).
I’ll leave the answer with Fowler (taken from the already mentioned book):
"The value of MVC lies in its two separations. Of these, the separation between presentation and model is one of the most important principles of software design, and the only time you shouldn’t follow it is in very simple systems where the model has no real behavior in it.
As soon as you have some non-visual logic, you should apply separation.
Unfortunately, a lot of UI frameworks make this separation difficult, and those that don’t are almost always taught without separation.
The separation between view and controller is less important, so I would only recommend doing it when it’s really useful."
About this here: "But you spoke so much of validation in the Model, should I do there?" , validation rule inherent only to the own Model should rather be done in the Model. The attributes, as you said yourself, follow this premise. Validations that involve data conferencing between Models should be made in Controller or through static classes (Helpers).
– Leonel Sanches da Silva
@Ciganomorrisonmendez I think the "No" really got strong in this placement. My intention was not that. About the Helpers I agree, I forgot to comment on the name, but I hope you don’t mind "copying" your text. p
– Randrade
A big problem that I see and that I commented on in the answer are people using Helpers like being a Service or using Repository as if it were a Helper.
– Randrade