Is it okay to pass a large amount of data from a controller to view with viewmodel in MVC3 ?Here'
I have code that passes almost 1MB of data from my controller to the view through a viewmodel each time a new page is called. I could optimize this slightly but I am wondering if this would be worth doing as the flow of data is all internal.
Typical of what I am doing in the controller is that I am getting all test results from an Azure datastore and then putting them in a new instance of a class. I am then passing this class and others onto a view. I guess I am not sure. Would the data be passed by reference or would actual data be moved from one place to another?
Anyone have any experience with this side of performance tuning for MVC3?
Here's a made up example. In this example it's nice and easy to pass the "TestData" class and contents to the view but then I just need a couple of items from this class. So I'm wondering if I should add logic in the controller and add fields in the view model for these items or just not bother and move across all the class data includi开发者_如何学Cng the data I don't need.
public class testIndexViewModel
{
public string Url { get; set; }
public PageMeta PageMeta { get; set; }
public TestData TestData { get; set; }
}
Thanks,
The ViewModel
class that you create gets added to the ViewDataDictionary
which your compiled custom View
class accesses through its base System.Web.Mvc.ViewPage<ViewModelType>
class. All that means is that I'm pretty sure your ViewModel
- once it's created - is always accessed by reference, and not copied around the place.
The only performance issue you could come across (I suppose) would be consequent to creating a 1MB object in the first place; how many of these objects are you likely to be creating, and how often?
Personally, I'd not worry about performance optimisation without first load testing and noticing that it's actually causing a problem. If it does and your application only really needs a fraction of the data at any one time, you can build in an optimisation then.
Finally, if you have code in your View to sift through your 1MB of data and pick out the sections you want to display, you might want to only pass the View the data it needs in order to make the code more readable, and better separate the Controller and View's responsibilities.
http://en.wikipedia.org/wiki/Premature_optimization#When_to_optimize
I would look at this from a user perspective.
How much data will be shown on the screen at anyone time?
If there's too much then there is a risk the data will become meaningless as the user will suffer from information overload.
I would try and use a filtering system of some description, maybe drop down lists for a user to specify as specific set of filters. That way in your controller you can filter down the data so your only returning a small subset of the 1MB set of data.
Alternatively if you want to return all the data I would suggest a more ajax style where only a small subset is returned and as the user scrolls more data is loaded via ajax - similar to how Google images works.
It wont make any difference if you load the 1 mb data and then extract specific data in a new model and pass that to the view. You will use the same amount of memory. As long as you do not bind against that huge blob in the view
精彩评论