At Wits End on Clean/Dirty within a Collection

At Wits End on Clean/Dirty within a Collection

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


bcrowell posted on Thursday, February 26, 2009

I've been working for several days on what seems like a basic problem. I have an editable collection of root objects. This collection is bound to a windows form list box. I select an item and in code grab a reference to an instance of the root object type. I then change one of the properties on the object. I have a checkbox bound to the isdirty property which switches on at that point as expected. I then do an applyedit to the object and then save it. At this point the isdirty flag turns off. All good.

However, when i select another item in the list box and then come back to selecting this original item it shows that it's marked dirty again. In fact, through testing, I've found that the item in the collection turns back to being marked isdirty as soon as I select another item. I feel like i must be missing something really obvious. Does anyone have any ideas on this before I tear the rest of my hair out? Thanks for all of the support.

Byron Crowell

ajj3085 replied on Friday, February 27, 2009

Don't call ApplyEdit on the BO itself. You must call it on the BindingSource to which the List is bound.

bcrowell replied on Friday, February 27, 2009

Thanks for your input, but it turns out this has nothing to do with binding sources. After 2 days of experimenting I have observed the following.

1) I populate a businesslistbase collection with root businessbaseobjects
2) I grab a reference to any one of the objects in the collection through a separate instance.
3) I change a property on the this instance.

From this point on, if I apply edit and save the instance, I can turn get IsDirty top be false on the instance object. But the corresponding item in the collection can NEVER be made to turn IsDirty off. Even if I save the entire collection object with apply edit before hand, the item that was changed will remain dirty indefinately. This is true even when I remove all binding sources from the project so I don't think it's a databinding issue.

Is anyone knows how to change of fix this I would be greatful. I need to be able to get that collection to show the items as clean without a reload of the collection.


RockfordLhotka replied on Friday, February 27, 2009

BusinessListBase is designed to contain child objects, not root objects. It doesn't surprise me that a root object won't work properly when contained in a BLB.

If you want a collection of root objects, either create your own collection type or use EditableRootListBase (ERLB).

ERLB is designed to act as a container for root objects, where changes to each root object is committed as the user moves off a row in a Windows Forms, WPF or Silverlight data grid control. It may work in other scenarios, but this is the only scenario it is designed to support.

If you have a different scenario, you should consider creating your own collection type to contain the root objects so you can manage those objects according to the requirements of your scenario.

Please remember that the data portal can work on nearly any serializable data type. In other words, you can create a collection of root objects something like this (off the top of my head):

[Serializable]
public class MyRootList : List<MyRootObjectType>
{
  public static MyRootList GetList()
  {
    return DataPortal.Fetch<MyRootList>();
  }

  public MyRootList Save()
  {
    return DataPortal.Update<MyRootList>(this);
  }

  private void DataPortal_Fetch()
  {
    // load collection with root objects
  }

  private void DataPortal_Update()
  {
    // save all root objects
  }
}

Obviously this implementation doesn't do anything fancy, but technically it should allow you to retrieve and save a collection of root objects. It won't support n-level undo, or fully support data binding, or pretty much anything else normally supported by BLB or ERLB, but it will support persistence through the data portal.

Copyright (c) Marimer LLC