Domanda

In clean MVC, I would gather the data from the database and pass it over to the view to be rendered.

Even if the view logic chooses not to render some elements. Which raises the following question:

Is is clean to prepare a container with all the database queries, but only fetch the data from within the view, if and when the view starts to iterate this container (it's iterable), instead of fetching the data "in the controller"?

Addendum

The container I'm passing over to the view is "kind of" a model, let's call it ExpensiveObjectProvider. The data just has not yet been fetched from the database when this container is passed to the view.

The objects encapsulated by the container could be expensive. The only reason I see to still do it in the controller is error handling (in case of an error with the database query, connectivity problems, etc). Do you think it would be a good trade-off to prefetch the first 1-2-3 objects from the database in the controller, then pass over the container to the view (if no exception occures) and let the view consume the prefetched data and fetch new items as the container is iterated?

È stato utile?

Soluzione

Typically database code does not go into the view, it will be in the model or a DAO the model depends on. Logic in the view will be hard to test, and very messy if database accessing code is in it too.

Altri suggerimenti

A view should not peform any such logic. The controller tells the view the data to display. The controller gets this data from the model.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top