Saving changes made to multiple business objects on click of Apply

Saving changes made to multiple business objects on click of Apply

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


adi posted on Monday, November 27, 2006

While looking for a good architecture bcoz of some bottlenecks in the present one, we found CSLA quite interesting and was trying to implement a small part of it. I tried to check if this kind of a problem has been answered before but couldn't find any. So, typing in here hoping somebody must have done this.

I have a form similar to Tools->Options of Visual Studio where different controls are displayed on clicking different treenodes. Finally on clicking the OK button, the changes are saved. Unlike in Visual Studio, whenever a treenode is clicked, my controls are populated using different business objects which are loaded with data from a database. The user can do any of the CRUD operations and they will be saved to DB only when the form's Apply button is clicked. On clicking the Cancel button, the operations are rolled back and the changed business objects are repopulated.

Is there any way to implement this using CSLA?

I would like to make the Apply button enabled when a value is changed for any of the business object, keep track of all the objects which are dirty and save them only when the Apply button is clicked.

Brian Criswell replied on Monday, November 27, 2006

You want to create an additional object whose responsibility is to make sure the objects are all saved or the save is rolled back if there is an exception.  This object will take all of the other objects as arguments to its factory method.  Its DataPortal_Update/Execute method could be marked with the [Transactional(TransactionScope)] attribute to ensure that all items are rolled back if there is an exception.

Because the apply button is dependent on several objects, I would recommend hooking up the PropertyChanged events on the objects to an event handler that sets the enabled state of the button based on whether any of the objects are dirty and all of the objects are valid.

So when the user clicks the apply button, you call AdditionalObject.SaveAllObjects(object1, object2, etc.).  This adds the objects to a criteria object which gets passed to the data portal.  The data portal calls the DataPortal_Update/Execute method (depending on whether you used a BusinessBase or CommandBase object), which was marked with the Transactional attribute.  Within the DataPortal_* method, you call object1.Save(), object2.Save, etc.

That should be enough information to at least get you started.

Copyright (c) Marimer LLC