10
I see in several code examples this, and even in the "standard" project that is created in visual studio, they leave a good part of the rule in the controller
Is it wrong? When to use it? What is the advantage and disadvantage?
10
I see in several code examples this, and even in the "standard" project that is created in visual studio, they leave a good part of the rule in the controller
Is it wrong? When to use it? What is the advantage and disadvantage?
8
Depends.
I think it’s best to categorize by type of rule to make it clear.
Yes, it is wrong. Data validation is a feature of the data type, therefore the responsibility of the Model.
No. It’s a function of Controller check if the record of another table actually exists before making any assignment. Controller’s responsibility is to harmonize data between Models and also between the presentation layer.
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 else a ActionFilter
.
Otherwise, there’s no problem using this in the Controller, as long as your Controller has proper transaction support.
Yes, much wrong. The Controller shall only provide the data for Views. Never mount HTML, for example.
This does not apply if the result returned by a Controller be it a file (for example, an image or a PDF), or some standardized data format (a JSON, an XML, and so on).
There is a caveat about Pdfs. There is a package called Razorpdf2 that mounts Pdfs at the level of View. This package is my own, so any questions or if you want to report bugs, you can contact me through the means available here on the site (mention, chat, etc.).
Because it is Gypsy, as I said, I saw a good part of the rule in controllers, there are also those who create classes "Service" (What I find complicated in some parts) and in controllers are very easy to govern and even apply DI to the reposiroty
I see no need for the layer of Service
, whereas the Controller
absorbs this function well.
And when you need to share certain rules between entities?
Well, my opinion: abandon this idea of using Service, because this design is hindering the functionality of the application components.
5
Preferably avoid doing this, of course there may be exceptions.
There are several arguments of why, if you have the same rule that will be used by another controller? Will you duplicate? Then moving to another class that will often be a Service will make it easier to reuse.
You are breaking the principle of sole responsibility of SOLID. Basically you’ll be letting your controller do more things than he should do.
But not to sound too inflexible, if you’re just making a phone book it might not be a problem, but when your phone book starts to grow it’s time to follow good practices that will prevent headaches and noodle codes.
Just opinionated, I disagree that the specific format of ASP.NET MVC goes against SOLID, considering that the project uses a good data abstraction framework, such as the Entity Framework. If logic needs to be repeated in more than one Controller, means that the project itself is flawed, for there are two Controllers performing the same action, which does not even justify the level of routes (since it is possible to define several sets of routes). Still in time, the approach about service and repository is prolific and the gain is small in Design.
I did not say that the format of ASP.NET MVC goes against SOLID, if the understanding of the answer was this then I was not clear. I said that putting business rules in your controller layer will be hurting some SOLID principles among them the principle of exclusive responsibility.
Browser other questions tagged asp.net-mvc-5 pattern-design
You are not signed in. Login or sign up in order to post.
Good, I never knew for sure how far a rule would be from a Controller or a Model (not only in . NET)
– Kazzkiq