I have simple view model, which is populating itself but the problem is that a webpage contains a lot of select lists and for every one select list I am calling database procedure for getting data list.
Is it possible, for performance, to execute database calls asynchronous or in parallel?
I have this kind of code :
// controller
public ActionResult Index()
{
model = new SampleViewModel();
model.Populate(database);
return View(model);
}
// view model
public class SampleViewModel
{
public SampleViewModel(DbContext db)
{
_list1 = context.Db.SqlQuery<SelectList1>("SELECT Id, Value FROM dbo.Table1").ToList();
_list2 = context.Db.SqlQuery<SelectList2>("SELECT Id, Value FROM dbo.Table2").ToList();
_list3 = context.Db.SqlQuery<SelectList3>("SELECT Id, Value FROM dbo.Table3").ToList();
_list4 = context.Db.SqlQuery<SelectList4>("SELECT Id, Value FROM dbo.Table4").ToList();
_list5 = context.Db.SqlQuery<SelectList5>("SELECT Id, Value FROM dbo.Table5").ToList();
}
private readonly List<SelectList1> _list1;
public int SelectedList1Id { get; set; }
public IEnumerable<SelectListItem> List1 { get { return new SelectList(_list1, "Id", "Value");} }
-//- _list2
-//- _list3
-//- _list4
-//- _list5
}
As you can see, _list3 is waiting for _list2 and _list2 is waiting for _list1 and this can slow request a lot. The reason why view model is populating itself is because in real scenario these select lists are related to each other and the model contains information about selected Ids and with these Ids I can rebuild the select lists for example if model validation failed.
Any idea? Can I use some async await approach and will it help me in this case against SQL Server 2008 ?