C# – Best method for async initialization of view model in MVVM pattern (WPF)

asynccmultithreadingperformancewpf

I'm working on a business application (C#, WPF, EF, MVVM). I need to load a bunch of items from database, create view models for them and put them in a window. Is there a way to create the view model in another thread or do something similar to speed up UI?

  1. The approximate average time needed to fetch all items from database = 1s
  2. The approximate average time needed to instantiate all view models = 3s

I'm doing the first part through Loaded event of each item. but the second part is the bottleneck. any insight would be appreciated.

Best Answer

Break up the views into separate tabs/fly-outs etc and only populate these bits when the tabs/fly-outs are actually switched to.

Further to the above only populate the first element i.e. SELECT 1 only then populate the rest in the view on a scroll or drop down operation etc..

Lazy Loading.

In other words this is some of the layered caching alluded too and i do not see why the ViewModel has to have the data in advance as long as it goes off and gets it whenever requested..