public static void BeginUpdate <T>(T obj, EventHandler <DataPortalResult <T> > callback, object userState) where T : IMobileObject { DataPortal <T> dp = new DataPortal <T>(); dp.UpdateCompleted += callback; dp.BeginUpdate(obj, userState); }
/// <summary> /// Saves the specified item in the list. /// </summary> /// <param name="index"> /// Index of the item to be saved. /// </param> /// <remarks> /// This method properly saves the child item, /// by making sure the item in the collection /// is properly replaced by the result of the /// Save() method call. /// </remarks> public virtual void SaveItem(int index) { T item = this[index]; var handleBusy = false; if (item.IsDeleted || (item.IsValid && item.IsDirty)) { T savable = item; // attempt to clone object ICloneable cloneable = savable as ICloneable; if (cloneable != null) { savable = (T)cloneable.Clone(); MethodCaller.CallMethodIfImplemented(item, "MarkBusy"); handleBusy = true; } // commit all changes int editLevel = savable.EditLevel; for (int tmp = 1; tmp <= editLevel; tmp++) { savable.AcceptChanges(editLevel - tmp, false); } // save object DataPortal <T> dp = new DataPortal <T>(); dp.UpdateCompleted += (o, e) => { if (handleBusy) { MethodCaller.CallMethodIfImplemented(item, "MarkIdle"); } if (e.Error == null) { T result = e.Object; if (item.IsDeleted) { //SafeRemoveItem will raise INotifyCollectionChanged event SafeRemoveItem(index); } else { for (int tmp = 1; tmp <= editLevel; tmp++) { result.CopyState(tmp, false); } SafeSetItem(index, result); #if SILVERLIGHT //Because SL Data Grid does not support replace action. // we have to artificially raise remove/insert events OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this[index], index)); #else OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, result, item)); #endif } #if SILVERLIGHT item.SaveComplete(result, null, null); #else item.SaveComplete(result); #endif OnSaved(result, null); } else { #if SILVERLIGHT item.SaveComplete(item, e.Error, null); #else item.SaveComplete(item); #endif OnSaved(item, e.Error); } }; dp.BeginUpdate(savable); } }
/// <summary> /// Starts an async operation to save the object to the database. /// </summary> /// <param name="handler"> /// Method called when the operation is complete. /// </param> /// <param name="userState">User state object.</param> public virtual void BeginSave(EventHandler <SavedEventArgs> handler, object userState) { if (this.IsChild) { var error = new InvalidOperationException(Resources.NoSaveChildException); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else if (EditLevel > 0) { var error = new InvalidOperationException(Resources.NoSaveEditingException); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else if (!IsValid) { Rules.ValidationException error = new Rules.ValidationException(Resources.NoSaveInvalidException); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else if (IsBusy) { var error = new InvalidOperationException(Resources.BusyObjectsMayNotBeSaved); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else { if (IsDirty) { if (userState == null) { DataPortal.BeginUpdate <T>(this, (o, e) => { T result = e.Object; OnSaved(result, e.Error, e.UserState); if (handler != null) { handler(result, new SavedEventArgs(result, e.Error, null)); } }); } else { DataPortal.BeginUpdate <T>(this, (o, e) => { T result = e.Object; OnSaved(result, e.Error, e.UserState); if (handler != null) { handler(result, new SavedEventArgs(result, e.Error, e.UserState)); } }, userState); } } else { OnSaved((T)this, null, userState); if (handler != null) { handler(this, new SavedEventArgs(this, null, userState)); } } } }
/// <summary> /// Starts an async operation to save the object to the database. /// </summary> /// <param name="forceUpdate"> /// If <see langword="true"/>, triggers overriding IsNew and IsDirty. /// If <see langword="false"/> then it is the same as calling Save(). /// </param> /// <param name="handler"> /// Method called when the operation is complete. /// </param> /// <param name="userState">User state data.</param> public virtual void BeginSave(bool forceUpdate, EventHandler <SavedEventArgs> handler, object userState) { if (forceUpdate && IsNew) { // mark the object as old - which makes it // not dirty MarkOld(); // now mark the object as dirty so it can save MarkDirty(true); } if (this.IsChild) { var error = new InvalidOperationException(Resources.NoSaveChildException); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else if (EditLevel > 0) { var error = new InvalidOperationException(Resources.NoSaveEditingException); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else if (!IsValid && !IsDeleted) { Rules.ValidationException error = new Rules.ValidationException(Resources.NoSaveInvalidException); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else if (IsBusy) { var error = new InvalidOperationException(Resources.BusyObjectsMayNotBeSaved); OnSaved(null, error, userState); if (handler != null) { handler(this, new SavedEventArgs(null, error, userState)); } } else { if (IsDirty) { MarkBusy(); if (userState == null) { DataPortal.BeginUpdate <T>(this, (o, e) => { T result = e.Object; OnSaved(result, e.Error, e.UserState); if (handler != null) { handler(result, new SavedEventArgs(result, e.Error, e.UserState)); } MarkIdle(); }); } else { DataPortal.BeginUpdate <T>(this, (o, e) => { T result = e.Object; OnSaved(result, e.Error, e.UserState); if (handler != null) { handler(result, new SavedEventArgs(result, e.Error, e.UserState)); } MarkIdle(); }, userState); } } else { OnSaved((T)this, null, userState); if (handler != null) { handler(this, new SavedEventArgs(this, null, userState)); } } } }