Asynchronous call previously hooked into BeginSave in Silverlight - CSLA 4.5

Asynchronous call previously hooked into BeginSave in Silverlight - CSLA 4.5

Old forum URL: forums.lhotka.net/forums/t/11617.aspx


skagen00 posted on Wednesday, October 03, 2012

I am basically doing my first tinkering with await and async, so I may have this incorrect.

Here's an example of something I need to refactor on the Silverlight side.

        public override void BeginSave(bool forceUpdate, EventHandler<Csla.Core.SavedEventArgs> handler, object userState)
        {
            // Need to update the lock status before we try to save.
            RefreshLockStatus((o, e) =>
                {
                    if (BusinessRules.GetBrokenRules().GetFirstBrokenRule(IsLockedProperty.Name) != null)
                    {
                        throw new CompanyException(Properties.BusinessRules.CannotSaveDueToLock);
                    }

                    base.BeginSave(forceUpdate, handler, userState);
                });
        }

Basically, we look to refresh the locked status of the record before initiating the save process such that the user can basically be given a broken rule message prior to the actual save initiating. 

I'm not sure that there is an easy mechanism to solve this now, is there?

I have refactored RefreshLockStatus to RefreshLockStatusAsync (is async).  My inclination was to plug into SaveAsync, but "await RefreshLockStatusAsync" is not correct syntax since the method itself isn't async.

        protected override System.Threading.Tasks.Task<CompanyObjectType> SaveAsync(bool forceUpdate, object userState, bool isSync)
        {
            await RefreshLockStatusAsync();

            // Check the impact of the refresh lock and do stuff.

      return base.SaveAsync(forceUpdate, userState, isSync);
        }

Is it something trivial I am missing?

skagen00 replied on Wednesday, October 03, 2012

Looks like this ends up compiling - once my project fully compiles I guess I will find out if this actually works.

        protected async override System.Threading.Tasks.Task<CompanyObjectType> SaveAsync(bool forceUpdate, object userState, bool isSync)
        {
            await RefreshLockStatusAsync();

     // Do stuff.

      return await base.SaveAsync(forceUpdate, userState, isSync);
        }

RockfordLhotka replied on Wednesday, October 03, 2012

Yes, this should be correct. Because SaveAsync is designed to return Task<T>, it can be marked with the async keyword (and the base class implementation is marked that way).

Copyright (c) Marimer LLC