Silverlight - Using async / await with Csla.Xaml.ViewModelBase<T>

Silverlight - Using async / await with Csla.Xaml.ViewModelBase<T>

Old forum URL:

Jaans posted on Monday, November 19, 2012

We have a few Silverlight projects using CSLA, and specifically a viewmodel base class that ultimately inherits from Csla.Xaml.ViewModelBase<T>

So I'm wondering how (if at all) one would use the async/await pattern with this view model. For example, the BeginRefresh( factoryWithCallback ) method expects a CSLA factory method that uses the DataPortalResult<T> handler callback.

If we are working towards a unified pattern that aims to do away with the DataPortalResult<T> callback and standardise on the the usual synchronous (.NET) factory together with the new async / await factories (for XAML based UIs - Silverlight, WPF, WinRT) then I'm curios to see how this impacts the ViewModelBase (which is still dependent on the old callback pattern.)

It does seem as if the SaveAsync method has been added to the view model base class at some point.


RockfordLhotka replied on Tuesday, November 20, 2012

To use async/await properly you need to use it all the way down the call stack. That means the UI code that initializes the viewmodel needs to await that operation.

You can't mark a constructor as async, nor can you await a constructor. So the old pattern of initializing the viewmodel via the constructor doesn't work anymore.

To overcome this, I added an InitAsync method to ViewModelBase. This method will await a protected DoInitAsync method.

So your UI code looks like this:

var vm = new MyViewModel();
this.DataContext = await vm.InitAsync();

and your viewmodel is initialized by overriding DoInitAsync:

protected override async Task<MyBusinessClass> DoInitAsync()

  return await MyBusinessClass.NewMyBusinessClassAsync();


Copyright (c) Marimer LLC