public void Save() {
// only do something if the object
implements
// ISavable
Csla.Core.ISavable savable = this.Data as Csla.Core.ISavable;
if ( savable != null )
IsBusy = true;
if ( IsAsynchronous ) {
System.Threading.ThreadPool.QueueUserWorkItem(
DoSave, savable );
}
else {
DoSave( savable );
IsBusy = false;
}
}
private void DoSave( object state ) {
Csla.Core.ISavable savable = state as Csla.Core.ISavable;
object result = savable;
Exception exceptionResult
= null;
try {
// apply edits in memory
Csla.Core.ISupportUndo undo = savable as Csla.Core.ISupportUndo;
if ( undo != null && _manageLifetime )
undo.ApplyEdit();
if ( !Csla.ApplicationContext.AutoCloneOnUpdate
) {
// clone the object if possible
ICloneable clonable =
savable as ICloneable;
if ( clonable != null )
savable = (Csla.Core.ISavable)clonable.Clone();
}
// save the clone
result = savable.Save();
if ( !ReferenceEquals( savable, this.Data )
&& !Csla.ApplicationContext.AutoCloneOnUpdate ) {
// raise Saved event from
original object
Core.ISavable original = this.Data as Core.ISavable;
if ( original != null ) {
if (
System.Windows.Application.Current.Dispatcher.CheckAccess() ) {
original.SaveComplete(
result );
}
else {
System.Windows.Application.Current.Dispatcher.Invoke(
new Action<object>(
(
obj ) => { original.SaveComplete( obj ); }
),
System.Windows.Threading.DispatcherPriority.Normal,
result
);
}
}
}
// start editing the resulting object
undo = result as Csla.Core.ISupportUndo;
if ( undo != null && _manageLifetime )
undo.BeginEdit();
}
catch ( Exception ex ) {
exceptionResult = ex;
}
if ( !System.Windows.Application.Current.Dispatcher.CheckAccess()
)
System.Windows.Application.Current.Dispatcher.Invoke(
new Action( () => { IsBusy = false; } ),
new object[] { }
);
// clear previous object
base.OnQueryFinished( null,
exceptionResult, null, null );
// return result to base class
base.OnQueryFinished( result, null, null, null );
}
We considered doing this, and decided against it. The reason we decided against it is because of the PropertyStatus issue you discovered - but in the broader sense.
It is quite possible for other controls, or UI code, or we-don't-know-what, to set up handlers for PropertyChanged events. They would all break if the Save() operation occurred on a background thread. This could cause massive confusion and unhappiness - and possibly unsolvable issues (think about third-party controls that handle the event for example).
Copyright (c) Marimer LLC