Undo with parent-child-grandchild object graph

Undo with parent-child-grandchild object graph

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


triplea posted on Thursday, April 17, 2008

Hi

I am a bit new to this... I have the following object graph:

Customer (Parent - BusinessBase)
   InvoiceList (Child - BusinessBaseList)
      Invoice (Child - BusinessBase)
         InvoiceLineList (Grandchild - BusinessBaseList)
            InvoiceLine (Grandchild - BusinessBase)

Now on the UI I have 2 grids, 1 for all invoices and 1 for the invoice lines of the selected invoice. I have a plus button on top of both grids. When pressed, they bring up a dialog which allows them to edit either the new Invoice or InvoiceLine. But the dialog has an "OK" and a "Cancel" button. When "OK" then the Invoice/Invoice line is added, when "Cancel" then undo the addition of the Invoice/InvoiceLine.
What I tried to do is the following (in the Invoice example):

_customer.BeginEdit();
Invoice invoice = _customer.Invoices.AddNew();
<... Display invoice on dialog. Return "Cancel" from dialog ...>
if (dialogResult == "Cancel")
   _customer.CancelEdit()
else
   _customer.ApplyEdit();

This didnt work. No errors but _customer still contains the new Invoice in its list. I tried various variations of the same theme but am either gettign errors or nothing happens. I have the 3.0 ebook in front of me but when it comes to child hierarachy then Begin/Cancel edit is called on the parent then the individual child. Is my approach completlty wrong or did I miss something out?

triplea replied on Thursday, April 17, 2008

I saw this very recent post: http://forums.lhotka.net/forums/thread/22867.aspx

This seems to be the golden rule: Never directly interact with a business object while it is data bound - only interact with the bindingsource.

Which is cool. But in the case above what should happen? Say we have the following bindingSources:

customerBindingSource (for parent Customer)
invoicesBindingSource (which binds to customerBindingSource and datamember Invoices)
invoiceLinesBindingSource (which binds to invoicesBindingSource and datamember Invoice)

Doing the following again produces no result (or error):

customerBindingSource.EndEdit();
<... get the Customer from customerBindingSource and add an Invoice to its list ...>
customerBindingSource.CancelEdit();

I think my approach is a bit blind (trying different approaches without actually knowing the results) so might be going round in circles here...

Forgot to mention I am using CSLA 3.0.3

Any help would be greatly appreciated :-)

Copyright (c) Marimer LLC