I believe it is not a class of the model layer because it is not a validation of data, but of processes. This line of reasoning is correct?
It depends. In question you said the following:
I’ve been reading some questions and answers about the MVC standard and I saw that they say that the treatment of business rules in the Controller layer is correct, such as: Check if a record already exists in the database before persisting it.
I admit I said something like that, but in the answers I stated that the database is managed by the Entity Framework, which would be the Framework that implements a complete repository.
In this case, what the Controller does is invoke the Entity Framework methods only, and does not perform the full treatment of a validation cycle.
The function of a Controller is basically to harmonize the data flows between the various components of an application. In the specific case of a validation, this place is the Model.
How can this be done?
On validation of processes
When logic is recurring (i.e., when it is used in more than one Controller) the correct approach is using a design standard called Helper.
What is a Helper?
Helper is a class, usually static, or an extension method (which is always static, therefore) that contains a recurring logic to several Controllers. He can be summoned at any time by any Controller, can use a data context (Entity Framework) or a repository (not-Entity Framework).
In it you can put what you call "process validation", with a complex sequence of steps, without necessarily putting this logic in a Controller.
Taking advantage
@Gus said the following in his reply:
It is very common for you to need to expose these methods through a web service, or to use the same business rules in a desktop application, for example.
This is true, but the idea is that in MVC6 the programmer writes a Controller for all presentation layers such as Desktop or Web Service.
There is, in fact, a limitation of a Controller MVC be almost strictly for a web project, but this should be simplified in vNext.
(By the date of this reply, vNext had not yet been officially released)
Hello @Gus, thank you for your reply. And what could be the names of that layer with business rules?
– James
@James where I work we usually put as follows. Company name_product layer_you can call Regranegocio or Negocio. The idea is to put a name that represents what you are proposing. A suggestive name.
– Gus
Business (Business) really seems to be the most appropriate. I’ll just wait a little longer to see if more answers emerge. Thank you for your response!
– James
I also have a preference for this kind of nomenclature, however is that your solution, if not already, may have a mobile application, which logic would receive a layer name App.
– James
@Gus, it’s out of scope, but I’d like to know if you’re familiar with software engineering, and if you could point me to a book with good teaching and content for a software study for someone who doesn’t know anything about it yet? Grateful!
– James