示例#1
0
        /// <summary>
        /// Accepts changes to the business object, and
        /// commits them by calling the object's Save()
        /// method.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method does nothing unless the object
        /// implements Csla.Core.ISavable.
        /// </para><para>
        /// If the object implements IClonable, it
        /// will be cloned, and the clone will be
        /// saved.
        /// </para><para>
        /// If the object supports n-level undo and
        /// ManageLifetime is true, then this method
        /// will automatically call ApplyEdit() and
        /// BeginEdit() appropriately.
        /// </para>
        /// </remarks>
        public void Save()
        {
            // only do something if the object implements
            // ISavable
            Csla.Core.ISavable savable = this.Data as Csla.Core.ISavable;
            if (savable != null)
            {
                object    result          = savable;
                Exception exceptionResult = null;
                try
                {
                    IsBusy = true;

                    // clone the object if possible
                    ICloneable clonable = savable as ICloneable;
                    if (clonable != null)
                    {
                        savable = (Csla.Core.ISavable)clonable.Clone();
                    }

                    // apply edits in memory
                    Csla.Core.ISupportUndo undo = savable as Csla.Core.ISupportUndo;
                    if (undo != null && _manageLifetime)
                    {
                        undo.ApplyEdit();
                    }


                    // save the clone
                    result = savable.Save();

                    if (!ReferenceEquals(savable, this.Data) && !Csla.ApplicationContext.AutoCloneOnUpdate)
                    {
                        // raise Saved event from original object
                        Core.ISavable original = this.Data as Core.ISavable;
                        if (original != null)
                        {
                            original.SaveComplete(result);
                        }
                    }

                    // start editing the resulting object
                    undo = result as Csla.Core.ISupportUndo;
                    if (undo != null && _manageLifetime)
                    {
                        undo.BeginEdit();
                    }
                }
                catch (Exception ex)
                {
                    exceptionResult = ex;
                }
                // clear previous object
                OnQueryFinished(null, exceptionResult, null, null);
                // return result to base class
                OnQueryFinished(result, null, null, null);
                IsBusy = false;
                OnSaved(result, exceptionResult, null);
            }
        }
        /// <summary>
        /// Saves the specified item in the list.
        /// </summary>
        /// <param name="index">
        /// Index of the item to be saved.
        /// </param>
        /// <remarks>
        /// This method properly saves the child item,
        /// by making sure the item in the collection
        /// is properly replaced by the result of the
        /// Save() method call.
        /// </remarks>
        public virtual T SaveItem(int index)
        {
            bool raisingEvents = this.RaiseListChangedEvents;

            this.RaiseListChangedEvents = false;
            _activelySaving             = true;

            T item   = default(T);
            T result = default(T);

            try
            {
                item   = this[index];
                result = item;
                T savable = item;

                // clone the object if possible
                ICloneable clonable = savable as ICloneable;
                if (clonable != null)
                {
                    savable = (T)clonable.Clone();
                }

                // commit all changes
                int editLevel = savable.EditLevel;
                for (int tmp = 1; tmp <= editLevel; tmp++)
                {
                    savable.AcceptChanges(editLevel - tmp, false);
                }

                // do the save
                result = (T)savable.Save();

                if (!ReferenceEquals(result, item))
                {
                    // restore edit level to previous level
                    for (int tmp = 1; tmp <= editLevel; tmp++)
                    {
                        result.CopyState(tmp, false);
                    }

                    // put result into collection
                    this[index] = result;
                }

                if (!ReferenceEquals(savable, item))
                {
                    // raise Saved event from original object
                    Core.ISavable original = item as Core.ISavable;
                    if (original != null)
                    {
                        original.SaveComplete(result);
                    }
                }

                OnSaved(result, null);
            }
            finally
            {
                _activelySaving             = false;
                this.RaiseListChangedEvents = raisingEvents;
            }
            this.OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
            return(result);
        }