private DataPortalClient.IDataPortalProxy GetDataPortalProxy(bool forceLocal) { if (forceLocal || ApplicationContext.IsOffline) { return(ApplicationContext.CreateInstanceDI <Csla.Channels.Local.LocalProxy>()); } else { return(DataPortalProxy); } }
/// <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.CreateInstanceDI<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; }