CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • Silverlight - Using async / await with Csla.Xaml.ViewModelBase<T>

    rated by 0 users
    Answered (Not Verified) This post has 0 verified answers | 1 Reply | 3 Followers

    Top 25 Contributor
    204 Posts
    Jaans posted on Mon, Nov 19 2012 3:25 AM

    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.

    Thanks,
    Jaans

    All Replies

    Top 10 Contributor
    9,475 Posts

    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();
    }

     

    Rocky

    Page 1 of 1 (2 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems