Exemplo n.º 1
0
        /// <summary>
        /// Starts an asynchronous data portal operation to
        /// update a business object.
        /// </summary>
        /// <typeparam name="T">
        /// Type of business object to update.
        /// </typeparam>
        /// <param name="obj">
        /// Business object to update.
        /// </param>
        public static async Task <T> UpdateAsync <T>(T obj)
            where T : IMobileObject
        {
            DataPortal <T> dp = new DataPortal <T>();

            return(await dp.UpdateAsync(obj));
        }
Exemplo n.º 2
0
        /// <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;

            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)
            {
                if (isSync)
                {
                    result = DataPortal.Update <T>((T)this);
                }
                else
                {
                    if (ApplicationContext.AutoCloneOnUpdate)
                    {
                        MarkBusy();
                    }
                    try
                    {
                        result = await DataPortal.UpdateAsync <T>((T)this);
                    }
                    finally
                    {
                        if (ApplicationContext.AutoCloneOnUpdate)
                        {
                            MarkIdle();
                        }
                    }
                }
            }
            else
            {
                result = (T)this;
            }
            OnSaved(result, null, userState);
            return(result);
        }
Exemplo n.º 3
0
        /// <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)
            {
                if (isSync)
                {
                    result = DataPortal.Update <T>((T)this);
                }
                else
                {
                    result = await DataPortal.UpdateAsync <T>((T)this);
                }
            }
            else
            {
                result = (T)this;
            }
            OnSaved(result, null, userState);
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Saves the object to the database.
        /// </summary>
        public virtual async Task <T> SaveAsync()
        {
            try
            {
                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)
                {
                    result = await DataPortal.UpdateAsync <T>((T)this);
                }
                else
                {
                    result = (T)this;
                }
                OnSaved(result, null, null);
                return(result);
            }
            catch (Exception ex)
            {
                OnSaved(null, ex, null);
                throw;
            }
        }
Exemplo n.º 5
0
        /// <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);
                try
                {
                    result = await DataPortal.UpdateAsync <T>((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);
                }
            }
        }