MVC - Datasource container in the view

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?

Answers


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.


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.


Need Your Help

Advantages of VS 2008 over VS 2005

visual-studio ide

Could somebody please name a few. I could given time, but this is for somebody else, and I'd also like some community input.

Bootstrap 3 segmented dropdown & button spacing

css twitter-bootstrap twitter-bootstrap-3

I'm trying to create a button group which is text input, dropdown then submit button.