Confused about EditLevels

Confused about EditLevels

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


DivisibleByZero posted on Wednesday, April 01, 2009

Ok I have some big confusion with EditLevel in my project. I am unable to call ApplyEdit on my root object because it fails. I created a small project to see if I could duplicate the problem but I was unable to, but it did make me scratch my head on the various EditLevel's my objects had.

Here is my object model:

Root : BusinessBase<Root>
    Id : Int32
    Children: Children

Children : BusinessListBase<Children, Chld>

Child : BusinessBase<Child>
   Id : Int32
   Grandchildren : Grandchildren

Grandchildren : BusinessListBase<Granchildren, Grandchild>

Grandchild: BusinessBase<Grandchild>
    Id : Guid

I then created a simple windows form with a dropdown and a gridview. I have 3 BindingSource objects in my form:
rootBindingSource
childrenBindingSource   - bound to dropdown list
grandchildrenBindingSource  - bound to data grid

in the form load I do Root.NewRoot() followed by a call to BindUI(). BindUI can only bind the root and the Children because Grandchildren is a child of a particular Child object.

BindUI() {
     root.BeginEdit();
    childrenBindingSource.DataSource = root.Children;
    rootBindingSOurce.DataSource = roo;
}

I then handle the CurrentChanged event of the BindingSource so I can bind Grandchildren:

childrenBindingSource_CurrentChanged(object sender, EventArgs e) {
    Child c = (Child)childrenBindingSource.Current;
    grandchildrenBindingSOurce.DataSource = c.Grandchildren;
}

I have a simple save method that will call the RebindUI(true, true) and save the object and Unbind all the data sources. This does the same thing as what the project tracker does...unbinding all 3 sources.  I set a breakpoint right before the call to BindUI and this is what the various edit levels are:

root.EditLevel - 2 // expected
root.Children.EditLevel - 1 // expected?
root.Children[0].EditLevel - 2 // expected - this is the Current item in the dropdown
root.Children[0].Grandchildren.EditLevel - 1 // expected?
root.Children[0].Grandchildren[0].EditLevel - 1
// not expected? shouldn't this be 2 since it is the current item in the grid

In this particular case the object is still savable but for some reason in my larger project I'm having somewhat similar results and it's causing it to have an EditLevel mismatch. Am I doing something wrong with binding the grandchildren? Anything else that jumps out at anyone?
   


RockfordLhotka replied on Wednesday, April 01, 2009

There are two sets of rules to consider: edit level, and data binding.

The edit level rules are pretty straightforward. For each BeginEdit() on an object, you must call CancelEdit() or ApplyEdit() on that object before calling any of those three methods on the object's parent (or grandparent, etc).

In other words, each level of editing for an object must be resolved before its parents go mucking around with their edit levels.

Similarly, the data binding rules are pretty simple. If an object is bound, it will have an elevated edit level. Period. The BindingSource control ensures that a bound object will have BeginEdit() called at all times, so the object is always in "edit mode".

So the only way to get an object's edit level reduced is to unbind it, and then call CancelEdit() or ApplyEdit(), because as long as it is bound you can't reduce the edit level.

Where this gets tricky is in the interaction between the edit level rules and the data binding rules, and that's where the content in the Using CSLA .NET 3.0 ebook (or the old threads on this topic) are invaluable.

Or you can use the CslaActionExtender in CSLA .NET 3.6 to do the work for you, which is probably a better option if it is available to you.

Copyright (c) Marimer LLC