How to remove an item from BusinessListBase and perform a list.Save() without cascading Save() into the other items in the list?

How to remove an item from BusinessListBase and perform a list.Save() without cascading Save() into the other items in the list?

Old forum URL: forums.lhotka.net/forums/t/11730.aspx


fryderyk posted on Thursday, November 29, 2012

The title may be a little confusing, let's dive into details:  I have a BLB inherited list model called Documents, and the child item model is a regular BusinessBase inherited model Document.  Consider the following situation:

The Documents list has 3 items, two of which is currently opened for editing (utilizing BeginEdit() ), the other one is not opened.

Now I'm trying to remove the closed document by calling list.Remove(doc), but the document file is not deleted since the actual filesystem delete operation is done in Child_DeleteSelf() in the Document class which is not fired until list.Save() is called. However I cannot simply call list.Save() because this will save all the other currently editing documents which is not intended.

Probably I made some silly mistakes somewhere, or is there a better way to accomplish this?

JonnyBee replied on Saturday, December 01, 2012

Hi,

So you want a different behavior than that of the standard lists - which is for the save to: 

  1. delete items in DeletedList that har IsNew = false
  2. insert/update items in list

You can override the RemoveItem method of the list to call a command object to do the actual immediate delete and the leave the child_delete method empty to do nothing in the child object.

fryderyk replied on Saturday, December 01, 2012

Really appreciated for the quick reply. I suppose you suggest doing this:

        protected override void RemoveItem(int index)
        {
            var item = this[index];
            base.RemoveItem(index);
            if (!item.IsNew)
            {
                File.Delete(item.FilePath);
                this.DeletedList.Remove(item); // since the underlying file is deleted,  it should not be undoable.
            }
        }


now whenever list.Remove(item) is called, the actual file delete is performed, eliminate the need to call list.Save(). But then the normal collection undo capability is lost. I don't like to alter the normal Remove(item) behavior, so I think for me, adding a separate Delete(item) function into the collection class is a more practical solution, like this:

        public void Delete(item)
        {
            if (item.IsNew)
            {
                this.Remove(item);
            }
            else
            {
                File.Delete(item.FilePath);
                this.Remove(item);
                this.DeletedList.Remove(item);
            }
        }

Is this an OK approach ?

JonnyBee replied on Sunday, December 02, 2012

The only difference in your code above is that you must call a specific method on the list for remove.

That doesn't stop anyone (potentially) using your code and call the regular Remove by using DataBinding or built-in Remove/Delete in a grid.

None of the solutions will support n-level undo functions in that a "deleted" item will be recovers . the file is already deleted.

 

fryderyk replied on Monday, December 03, 2012

Luckily no grid in my circumstances, will use the code from my previous reply.

Copyright (c) Marimer LLC