As always, there is more than one way to hide a programmer. As the saying goes, right?
A few years ago, when I first started using the MVC framework, I kept trying to understand what the gold standard is, for which every letter should be responsible. There are many opinions, but ultimately it is up to you and your team to find out what works for you.
I would say that connecting to a database in your view or is a bad practice. Some people open connections in their models to retrieve data. Not me. When I think that the model is responsible for this simply:
- Data to be displayed in the user interface
- Data necessary for the correct assembly of the user interface (for example, a
bool to conditionally display some parameters)
I often refer to my models as "self-sufficient." This means that my models should have all the data needed by the time they get into the view. I allow my controller to access database connections, API calls, LDAP requests, etc. Obviously, my controller does not contain all the code for these methods (I have the appropriate specialized libraries to handle different requirements), but it is a broker between different data sources.
What a pleasure to do with the architecture of your applications in such a way that you are sure that your hard climb is done. You know that when you call return View(model) , you have all the necessary data. You do not need to troubleshoot faulty requests (or slow API calls, etc.) from your model or view. When this line refers to what each part is responsible for, this is what makes the frame useful to me.
Remember, these are my opinions on how to use the structure. I am not saying this is the only solution. Your development team may find something more useful, but, while maintaining this discipline, it has worked well for me for several years.
Justin helgerson
source share