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"?


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?


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

Where should i put the DbContext? n-tier-architecture dbcontext

Was wondering in which layer project i should put the DbContext implementation if i am working with an MVC 3 project which integrate 3 layers ( BusinessLogic, Persistence, Domain)

Extract date value from string in php with regular expression

php regex

I have a string as mentioned below and i want to extract date from this string . Which regular expresion will be used for the same.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.