/// <summary> /// Override this method to create a new object that is added /// to the collection. /// </summary> protected override C AddNewCore() { var dp = ApplicationContext.CreateInstance <DataPortal <C> >(); var item = dp.CreateChild(); Add(item); return(item); }
/// <summary> /// Adds a new item to the list. /// </summary> /// <returns>The added object</returns> protected override T AddNewCore() { var dp = ApplicationContext.CreateInstance <DataPortal <T> >(); T item = dp.Create(); Add(item); return(item); }
/// <summary> /// Adds a new item to the list. /// </summary> /// <returns>The added object</returns> protected override object AddNewCore() { var dp = ApplicationContext.CreateInstance <DataPortal <T> >(); T item = dp.Create(); Add(item); this.OnAddingNew(new AddingNewEventArgs(item)); return(item); }
/// <summary> /// Saves the object to the database. /// </summary> /// <param name="userState">User state data.</param> /// <param name="isSync">True if the save operation should be synchronous.</param> protected virtual async Task <T> SaveAsync(object userState, bool isSync) { T result; if (this.IsChild) { throw new InvalidOperationException(Resources.NoSaveChildException); } if (_editLevel > 0) { throw new InvalidOperationException(Resources.NoSaveEditingException); } if (!IsValid) { throw new Rules.ValidationException(Resources.NoSaveInvalidException); } if (IsBusy) { throw new InvalidOperationException(Resources.BusyObjectsMayNotBeSaved); } if (IsDirty) { var dp = ApplicationContext.CreateInstance <DataPortal <T> >(); if (isSync) { result = dp.Update((T)this); } else { result = await dp.UpdateAsync((T)this); } } else { result = (T)this; } OnSaved(result, null, userState); return(result); }
protected virtual void Child_Update(params object[] parameters) { using (LoadListMode) { var dp = ApplicationContext.CreateInstance <DataPortal <C> >(); foreach (var child in DeletedList) { dp.UpdateChild(child, parameters); } DeletedList.Clear(); foreach (var child in this) { if (child.IsDirty) { dp.UpdateChild(child, parameters); } } } }
/// <summary> /// Initializes a new instance of the <see cref="LazySingleton<T>"/> class. /// Will use the default public constructor to create an instance of T (the value) /// </summary> public LazySingleton() { _delegate = () => (T)ApplicationContext.CreateInstance(typeof(T)); }
/// <summary> /// Saves the specified item in the list. /// </summary> /// <param name="index">Index of item to be saved.</param> /// <param name="delete">true if the item should be deleted.</param> protected virtual async Task SaveItemAsync(int index, bool delete) { T item = this[index]; var handleBusy = false; if ((item.IsDeleted || delete) || (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); } if (delete) { savable.Delete(); } Exception error = null; T result = default(T); var dp = ApplicationContext.CreateInstance <DataPortal <T> >(); try { result = await dp.UpdateAsync((T)savable); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 0) { error = ex.InnerExceptions[0]; } else { error = ex; } } catch (Exception ex) { error = ex; } finally { if (handleBusy) { MethodCaller.CallMethodIfImplemented(item, "MarkIdle"); } } // update index - this may have changed under the duration of async call index = IndexOf(item); if (error == null && result != null) { if (savable.IsDeleted) { //SafeRemoveItem will raise INotifyCollectionChanged event SafeRemoveItem(index); } else { for (int tmp = 1; tmp <= editLevel; tmp++) { result.CopyState(tmp, false); } SafeSetItem(index, result); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index)); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this[index], index)); } item.SaveComplete(result); OnSaved(result, null); } else { item.SaveComplete(item); OnSaved(item, error); } } }
/// <summary> /// Saves the object to the database. /// </summary> /// <param name="forceUpdate"> /// If true, triggers overriding IsNew and IsDirty. /// If false then it is the same as calling Save(). /// </param> /// <param name="userState">User state data.</param> /// <param name="isSync">True if the save operation should be synchronous.</param> protected async virtual Task <T> SaveAsync(bool forceUpdate, object userState, bool isSync) { 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); } T result = default; if (this.IsChild) { throw new InvalidOperationException(Resources.NoSaveChildException); } if (EditLevel > 0) { throw new InvalidOperationException(Resources.NoSaveEditingException); } if (!IsValid && !IsDeleted) { throw new Rules.ValidationException(Resources.NoSaveInvalidException); } if (IsBusy) { throw new InvalidOperationException(Resources.BusyObjectsMayNotBeSaved); } if (IsDirty) { var dp = ApplicationContext.CreateInstance <DataPortal <T> >(); if (isSync) { result = dp.Update((T)this); } else { if (ApplicationContext.AutoCloneOnUpdate) { MarkBusy(); } try { result = await dp.UpdateAsync((T)this); } finally { if (ApplicationContext.AutoCloneOnUpdate) { if (result != null) { result.MarkIdle(); } MarkIdle(); } } } } else { result = (T)this; } OnSaved(result, null, userState); return(result); }