Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 including 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,

like image 406
TonyG Avatar asked May 26 '11 13:05

TonyG


1 Answers

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

like image 109
Steve Wilkes Avatar answered Oct 02 '22 02:10

Steve Wilkes