/// <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 YYT.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 YYT.Core.ISavable savable = this.Data as YYT.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 = (YYT.Core.ISavable)clonable.Clone(); } // apply edits in memory YYT.Core.ISupportUndo undo = savable as YYT.Core.ISupportUndo; if (undo != null && _manageLifetime) { undo.ApplyEdit(); } // save the clone result = savable.Save(); if (!ReferenceEquals(savable, this.Data) && !YYT.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 YYT.Core.ISupportUndo; if (undo != null && _manageLifetime) { undo.BeginEdit(); } } catch (Exception ex) { exceptionResult = ex; } // clear previous object base.OnQueryFinished(null, exceptionResult, null, null); // return result to base class base.OnQueryFinished(result, null, null, null); IsBusy = false; } }
/// <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() { YYT.Core.ISupportUndo undo = this.Data as YYT.Core.ISupportUndo; if (undo != null && _manageLifetime) { IsBusy = true; undo.CancelEdit(); undo.BeginEdit(); IsBusy = false; } }
/// <summary> /// Binds a business object to the BindingSource. /// </summary> /// <param name="objectToBind"> /// Business object. /// </param> public void Bind(object objectToBind) { YYT.Core.ISupportUndo root = objectToBind as YYT.Core.ISupportUndo; if (root != null) { root.BeginEdit(); } _Source.DataSource = objectToBind; SetEvents(true); ResetBindings(false); }
/// <summary> /// Applies changes to the business object. /// </summary> public void Apply() { SetEvents(false); YYT.Core.ISupportUndo root = _Source.DataSource as YYT.Core.ISupportUndo; Unbind(false); EndEdit(); if (root != null) { root.ApplyEdit(); } }
/// <summary> /// Cancels changes to the business object. /// </summary> /// <param name="businessObject"></param> public void Cancel(object businessObject) { SetEvents(false); YYT.Core.ISupportUndo root = _Source.DataSource as YYT.Core.ISupportUndo; Unbind(true); if (root != null) { root.CancelEdit(); } Bind(businessObject); }
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; 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; MethodInfo[] methods = request.ObjectType.GetMethods(flags); foreach (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 (YYT.DataPortalException ex) { exceptionResult = ex.BusinessException; } catch (Exception ex) { exceptionResult = ex; } } catch (Exception ex) { exceptionResult = ex; } if (request.ManageObjectLifetime && result != null) { YYT.Core.ISupportUndo undo = result as YYT.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[] { }); } // return result to base class base.OnQueryFinished(result, exceptionResult, null, null); }