Exemplo n.º 1
0
        /// <summary>
        /// Saves the object to the database.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Calling this method starts the save operation, causing the object
        /// to be inserted, updated or deleted within the database based on the
        /// object's current state.
        /// </para><para>
        /// If <see cref="Core.BusinessBase.IsDeleted" /> is <see langword="true"/>
        /// the object will be deleted. Otherwise, if <see cref="Core.BusinessBase.IsNew" />
        /// is <see langword="true"/> the object will be inserted.
        /// Otherwise the object's data will be updated in the database.
        /// </para><para>
        /// All this is contingent on <see cref="Core.BusinessBase.IsDirty" />. If
        /// this value is <see langword="false"/>, no data operation occurs.
        /// It is also contingent on <see cref="Core.BusinessBase.IsValid" />.
        /// If this value is <see langword="false"/> an
        /// exception will be thrown to indicate that the UI attempted to save an
        /// invalid object.
        /// </para><para>
        /// It is important to note that this method returns a new version of the
        /// business object that contains any data updated during the save operation.
        /// You MUST update all object references to use this new version of the
        /// business object in order to have access to the correct object data.
        /// </para><para>
        /// You can override this method to add your own custom behaviors to the save
        /// operation. For instance, you may add some security checks to make sure
        /// the user can save the object. If all security checks pass, you would then
        /// invoke the base Save method via <c>base.Save()</c>.
        /// </para>
        /// </remarks>
        /// <returns>A new object containing the saved values.</returns>
        public virtual T Save()
        {
            T result;

            if (this.IsChild)
            {
                throw new NotSupportedException(Resources.NoSaveChildException);
            }
            if (EditLevel > 0)
            {
                throw new Validation.ValidationException(Resources.NoSaveEditingException);
            }
            if (!IsValid)
            {
                throw new Validation.ValidationException(Resources.NoSaveInvalidException);
            }
            if (IsDirty)
            {
                result = (T)DataPortal.Update(this);
            }
            else
            {
                result = (T)this;
            }
            OnSaved(result);
            return(result);
        }
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>
        /// <remarks>
        /// <para>
        /// Calling this method starts the save operation, causing the object
        /// to be inserted, updated or deleted within the database based on the
        /// object's current state.
        /// </para><para>
        /// If <see cref="Core.BusinessBase.IsDeleted" /> is <see langword="true"/>
        /// the object will be deleted. Otherwise, if <see cref="Core.BusinessBase.IsNew" />
        /// is <see langword="true"/> the object will be inserted.
        /// Otherwise the object's data will be updated in the database.
        /// </para><para>
        /// All this is contingent on <see cref="Core.BusinessBase.IsDirty" />. If
        /// this value is <see langword="false"/>, no data operation occurs.
        /// It is also contingent on <see cref="Core.BusinessBase.IsValid" />.
        /// If this value is <see langword="false"/> an
        /// exception will be thrown to indicate that the UI attempted to save an
        /// invalid object.
        /// </para><para>
        /// It is important to note that this method returns a new version of the
        /// business object that contains any data updated during the save operation.
        /// You MUST update all object references to use this new version of the
        /// business object in order to have access to the correct object data.
        /// </para><para>
        /// You can override this method to add your own custom behaviors to the save
        /// operation. For instance, you may add some security checks to make sure
        /// the user can save the object. If all security checks pass, you would then
        /// invoke the base Save method via <c>base.Save()</c>.
        /// </para>
        /// </remarks>
        /// <returns>A new object containing the saved values.</returns>
        public virtual T Save()
        {
            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)
            {
                result = (T)DataPortal.Update(this);
            }
            else
            {
                result = (T)this;
            }
            OnSaved(result, null, null);
            return(result);
        }
Exemplo n.º 4
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.º 5
0
        /// <summary>
        /// Called by the business object's Save() method to
        /// insert, update or delete an object in the database.
        /// </summary>
        /// <remarks>
        /// Note that this method returns a reference to the updated business object.
        /// If the server-side DataPortal is running remotely, this will be a new and
        /// different object from the original, and all object references MUST be updated
        /// to use this new object.
        /// </remarks>
        /// <typeparam name="T">Specific type of the business object.</typeparam>
        /// <param name="obj">A reference to the business object to be updated.</param>
        /// <returns>A reference to the updated business object.</returns>
        public static T Update <T>(T obj)
        {
            var dp = new DataPortal <T>();

            return(dp.Update(obj));
        }