Problem with ListChanged Event?

Posts   
 
    
ElQueso avatar
ElQueso
User
Posts: 27
Joined: 08-Oct-2005
# Posted on: 05-Jun-2006 06:06:19   

I encountered an issue that has been causing a few little bugs wink

I have a loop that loops through each entity in an entity collection and removes them using this line (using a counter):

myEntityCollection.RemoveAt(0);

I have trap the list changed event, looking for a deleted action. In this case I do some unit of work stuff to remove some sub-entities and sub-entity collections (only when the user saves) for old entities that may have data. I figured the EntityCollection ListChanged event would be the perfect place to do that.

However, I figured out that the index coming in via the event argments is indeed the index of the old entity in the collection that was removed, the problem is the entity no longer exists in the list at this point, so of course you get the new entity now that's first in the list instead of the one you deleted and things go downhill from there at a very rapid pace.

I realize now that I should have been performing the unit of work stuff in the BeforeRemove event, but it got me to thinking.

What happens if an error occurs in the ListChanged event after the BeforeRemove event is fired? It would be a pain to figure out what got added to the unit of work and undo that, so I have some entities and collections that are going to be removed from the database on a save.

I'm wondering why the deleted entity isn't given as an event arg parameter in the ListChanged event? Let me have it long enough to add the things I need to unit of work, then you can destroy it.

Or am I doing something really silly? Is there some kind of transaction bookmark functionality in uow maybe, that I'm not seeing? Even with that, though, it still seems cleaner to send the deleted entity in the ListChanged event.

Is the Deleted action in a ListChanged event just to know to ignore it?

I'd appreciate any thoughts or direction on this simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 05-Jun-2006 13:31:05   

I have to admit, I didn't completely understand the business scenario, but I may answer the following questions:

I'm wondering why the deleted entity isn't given as an event arg parameter in the ListChanged event? Let me have it long enough to add the things I need to unit of work, then you can destroy it.

That's because the ListChanged is an implementation of the IBindingList.ListChanged Event

Is there some kind of transaction bookmark functionality in uow maybe, that I'm not seeing?

You may use transaction's savepoints along with the UOW calls, please check the following thread: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=4864

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 05-Jun-2006 15:02:55   

Hi,

You can try the following: Add each entity in the entitycollection to an array in the same order that they are in the entitycollection. Process the entities in the entitycollection from the last to the first, use the index that you receive to get the deleted entity from the array.