This question is part B of my other question here:
Inefficient MVC ViewModel Making Multiple Calls to the Database?
I knew multiple calls to the db was a bad idea. But, I guess my real question is if I use my last chunk of code or speti43's example let's say like this in my model constructor to eliminate multiple db calls:
Public Class OrdersSummary{
...
Public ???? GetOrders(){
var ordersInMemory = orders.ToList();
decimal? GrossProfitTotal = ordersInMemory.Sum(s => s.Profit);
decimal? CommissionTotal = ordersInMemory.Sum(s => s.Commission);
decimal? NetProfitTotal = GrossProfitTotal + CommissionTotal;
return ?????
}
}
How do I pass those seperate but similar pieces of data to the controller? I was trying to use a tuple like this based on some other SO article recommendation here:
How to return multiple values from a function in C# (ASP.NET)?
public Tuple<IEnumerable<dynamic>, decimal?, decimal?, decimal?> GetOrders(string sortOrder, string searchString)
{
...
return new Tuple<IEnumerable<dynamic>,decimal?,decimal?,decimal?> (ordersInMemory, GrossProfitTotal, CommissionTotal, NetProfitTotal);
}
Does this sound reasonable? Then I have no idea how to reference the tuple in my controller:
var ordersummary = new OrdersSummary();
var viewModel = new OrderSummary
{
???? cause I have no idea how to reference the tuple
};
return View(viewModel);
The other issue is I'm not entirely sure I'm building the properties correct in my view model. Currently I have it like this for the four pieces of data and then individual properties of the Orders list:
public class OrderSummary
{
public IEnumerable<dynamic> Orders { get; set; }
public decimal GrossProfitTotal { get; set; }
public decimal CommissionTotal { get; set; }
public decimal NetProfitTotal { get; set; }
//Orders Table Properties
public int OrderNumber { get; set; }
public DateTime OpenTime { get; set; }
//more properties here that correspond to the Orders table.
}
It seems like I might need to structure the tuple properties different in the view model? If so, what syntax?
I think I might know what goes in the View if I knew what goes in the controller. Is this even the correct way to go about it? I got this whole view model structure idea from step 8 on the MVCMUsicStore tutorial:
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8