Example #1
0
 /// <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));
             }
         }
     }
 }
Example #2
0
        /// <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));
                    }
                }
            }
        }