Why does moving the location of a Child BO in a collection make it dirty?

Why does moving the location of a Child BO in a collection make it dirty?

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


BBM posted on Tuesday, February 06, 2007

Hi,

I'm trying to implement a simple swap routine of elements within an EditableChildCollection BO.  The contents of the collection are EditableChild BO's.  The code is located in the EditableChildCollection BO and looks like this...

Private Sub SwapElements(ByVal indx1 As Integer, ByVal indx2 As Integer)

Dim t As EditableChildBO

t = Me(indx2)

Me.SetItem(indx2, Me(indx1))

Me.SetItem(indx1, t)

End Sub

Prior to this code running, IsDirty is false for the BO's at both indx1 and indx2.  After it runs, IsDirty is True for both, even though nothing has changed except their location within the containing collection.

Why is this and how do I get these BO's "clean" again.  I've tried setting RaiseListChangedEvents to false, and also using an explicit BeginEdit / ApplyEdit on each object as it is moved.  No luck.  

Thanks for your help.  I'm using Version 2.02 of CSLA if that makes any difference.

BBM 

ajj3085 replied on Tuesday, February 06, 2007

Check out the code in SetItem.

Normally, SetItem assumes the current item in the position is being removed, and thus is copied to the DeletedList which also sets the BO's IsDeleted flag to true.  You have to do this, because what if you're just setting the item without a swap operation, the old object no longer part of the list and thus needs to be deleted.

Now, as to a solution.. instead of using SetItem, set the items directly using the protected Items property which is inhertied from Collection<T> (the ultimate super of BLB).

HTH
Andy

BBM replied on Tuesday, February 06, 2007

Andy,

Thank you VERY MUCH for your help.  I'm not sure I would have ever found the "Items" property on my own.   When I wrote my post, I actually had already tried to use the default property for the collection (me(indx1)) and the "explicit" item property (me.item(indx1)) without success.

I'm still not exactly sure why "Items" works.  The only thing I can think of is that the Item property must call SetItem() to do its work, which is really the SetItem override in BLB that you pointed out.  So Items() must use some other method to set  the collection contents that doesn't call SetItem and therefore doesn't run afoul of the DeletedList?

Like I say, I don't know if I would have ever discovered this difference between the Items and Item properties in BLB without your help.   Very handy to know. 

Thanks again.

BBM

ajj3085 replied on Wednesday, February 07, 2007

BBM,

I think you are correct; the indexing operator Item[ index ] calls SetItem to actually set the item.

The Items property is a protected List<T> that actually stores as the items; Collection<T> doesn't actually store the items itself, it wraps List<T> and delegates calls to the wrapped instance.

I actually had to go up the hierarchy checking out members to discover that.

Andy

Copyright (c) Marimer LLC