I see in several code examples this, and even in the "default" project that is created in visual studio, they leave a good part of the rule in the controller
Is it wrong? When to use this? What is the advantage and disadvantage?
I see in several code examples this, and even in the "default" project that is created in visual studio, they leave a good part of the rule in the controller
Is it wrong? When to use this? What is the advantage and disadvantage?
It depends.
I think it's best to categorize by rule type to be clearer.
Yes, it is wrong. Data validation is a characteristic of the data type, therefore the Model responsibility.
No. It is the function of Controller to check if the record of another table actually exists before making any assignment. The Controller's responsibility is to harmonize the data between Models and also between the presentation layer.
It depends. If the type of data to be audited follows a format and data pattern, it is best to use a library (such as those that work with aspects) that intercept the data, or a ActionFilter
.
Otherwise, there is no problem in using this in the Controller as long as your Controller has adequate support for transactions.
Yes, very wrong. The Controller should only provide the data for Views . Never mount HTML, for example.
This does not apply if the result returned by a Controller is a file (for example, an image or a PDF), or some standardized data format (JSON, XML, and so on). onwards).
There is a caveat about PDFs. There is a package called RazorPDF2 that assembles PDFs at View level. This package is my own, so any doubt or if you want to report bugs, you can contact me through the means available here on the site (mention, chat, etc.).
Preferably avoid doing this, of course there may be exceptions.
There are several reasons why, if you have the same rule that will be used by another controller ? Will you duplicate? So moving to another class that will often be a Service will make it easier for you to reuse.
You are hurting the principle of sole responsibility of SOLID . Basically you're letting your controller do more things than it should.
But to not seem too inflexible, if you're just making a phone book this might not be a problem, but when your phone book begins to grow it's time to follow good practices that will avoid headaches and macaroni codes.