/// <summary> /// Accepts changes to the business object, and /// commits them by calling the object's Save() /// method. /// </summary> /// <remarks> /// <para> /// This method does nothing unless the object /// implements Csla.Core.ISavable. /// </para><para> /// If the object implements IClonable, it /// will be cloned, and the clone will be /// saved. /// </para><para> /// If the object supports n-level undo and /// ManageLifetime is true, then this method /// will automatically call ApplyEdit() and /// BeginEdit() appropriately. /// </para> /// </remarks> public void Save() { // only do something if the object implements // ISavable Csla.Core.ISavable savable = this.Data as Csla.Core.ISavable; if (savable != null) { object result = savable; Exception exceptionResult = null; try { IsBusy = true; // clone the object if possible ICloneable clonable = savable as ICloneable; if (clonable != null) { savable = (Csla.Core.ISavable)clonable.Clone(); } // apply edits in memory Csla.Core.ISupportUndo undo = savable as Csla.Core.ISupportUndo; if (undo != null && _manageLifetime) { undo.ApplyEdit(); } // 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) { original.SaveComplete(result); } } // start editing the resulting object undo = result as Csla.Core.ISupportUndo; if (undo != null && _manageLifetime) { undo.BeginEdit(); } } catch (Exception ex) { exceptionResult = ex; } // clear previous object OnQueryFinished(null, exceptionResult, null, null); // return result to base class OnQueryFinished(result, null, null, null); IsBusy = false; OnSaved(result, exceptionResult, null); } }
/// <summary> /// Cancels changes to the business object, returning /// it to its previous state. /// </summary> /// <remarks> /// This metod does nothing unless ManageLifetime is /// set to true and the object supports n-level undo. /// </remarks> public void Cancel() { Csla.Core.ISupportUndo undo = this.Data as Csla.Core.ISupportUndo; if (undo != null && _manageLifetime) { IsBusy = true; undo.CancelEdit(); undo.BeginEdit(); IsBusy = false; } }
/// <summary> /// Refresh the ObjectInstance by calling the /// supplied factory. /// </summary> /// <typeparam name="T">Type of ObjectInstance</typeparam> /// <param name="factory">Async data portal or factory method</param> /// <returns></returns> public async void Refresh <T>(Func <Task <T> > factory) { T result = default(T); Exception exceptionResult = null; // invoke factory method try { result = await factory(); } catch (Csla.DataPortalException ex) { exceptionResult = ex.BusinessException; } catch (System.Reflection.TargetInvocationException ex) { if (ex.InnerException != null) { exceptionResult = ex.InnerException; var dpe = exceptionResult as Csla.DataPortalException; if (dpe != null && dpe.BusinessException != null) { exceptionResult = dpe.BusinessException; } } else { exceptionResult = ex; } } catch (Exception ex) { exceptionResult = ex; } if (ManageObjectLifetime && result != null) { Csla.Core.ISupportUndo undo = result as Csla.Core.ISupportUndo; if (undo != null) { undo.BeginEdit(); } } if (!_endInitCompete && exceptionResult != null) { _endInitError = true; } // return result to base class OnQueryFinished(result, exceptionResult, (o) => { IsBusy = false; return(null); }, null); }
private void DoQuery(object state) { QueryRequest request = (QueryRequest)state; object result = null; Exception exceptionResult = null; object[] parameters = new List <object>(request.FactoryParameters).ToArray(); try { // get factory method info BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy; System.Reflection.MethodInfo factory = request.ObjectType.GetMethod( request.FactoryMethod, flags, null, MethodCaller.GetParameterTypes(parameters), null); if (factory == null) { // strongly typed factory couldn't be found // so find one with the correct number of // parameters int parameterCount = parameters.Length; System.Reflection.MethodInfo[] methods = request.ObjectType.GetMethods(flags); foreach (System.Reflection.MethodInfo method in methods) { if (method.Name == request.FactoryMethod && method.GetParameters().Length == parameterCount) { factory = method; break; } } } if (factory == null) { // no matching factory could be found // so throw exception throw new InvalidOperationException( string.Format(Resources.NoSuchFactoryMethod, request.FactoryMethod)); } // invoke factory method try { result = factory.Invoke(null, parameters); } catch (Csla.DataPortalException ex) { exceptionResult = ex.BusinessException; } catch (System.Reflection.TargetInvocationException ex) { if (ex.InnerException != null) { exceptionResult = ex.InnerException; var dpe = exceptionResult as Csla.DataPortalException; if (dpe != null && dpe.BusinessException != null) { exceptionResult = dpe.BusinessException; } } else { exceptionResult = ex; } } catch (Exception ex) { exceptionResult = ex; } } catch (Exception ex) { exceptionResult = ex; } if (request.ManageObjectLifetime && result != null) { Csla.Core.ISupportUndo undo = result as Csla.Core.ISupportUndo; if (undo != null) { undo.BeginEdit(); } } //if (!System.Windows.Application.Current.Dispatcher.CheckAccess()) // System.Windows.Application.Current.Dispatcher.Invoke( // new Action(() => { IsBusy = false; }), // new object[] { }); if (!_endInitCompete && exceptionResult != null) { _endInitError = true; } // return result to base class OnQueryFinished(result, exceptionResult, (o) => { IsBusy = false; return(null); }, null); }