I'm binding a few levels of data to xamDataGrid, and I needed to identify a good point/event to trigger a service call to persist the data. My unit of work will be based on the outermost object in the hierarchy, and I don't want multiple dirty objects to accumulate because the unit of work would be too large. The outer object's RecordUpdated() event will not work because a user can modify the inner objects without changing the outer object.
The only thing I can think of at the moment is to have a dirty flag that bubbles up the hierarchy, and to identify the best event to persist the dirty outermost object. A focus change or record deactivation event might be tricky because hierarchies can be expanded, and the user can touch different levels of data in and out of the outermost object (an additional issue is how to handle the dirty flag when the user cancels an edit -- admittedly a little tricky when dealing with the hierarchical dirty flag).
Any ideas are appreciated. Thanks!
Hello Darryl,
Thank you for your post. I have been looking into it and I can suggest you use the RecordUpdated event, but in its handler you can make a check from if the event is fired from the first level of the hierarchy by adding this code:
if (e.Record.NestingDepth == 0) { //Do something... }
Hope this helps you.
Excellent suggestion Stefan. I actually ended up doing something very close to that, but in the RecordActivating event. Because hierarchies can be expanded, I could update a child record under one parent, and then update another child under another parent, and I would want to know that I'm done with the prior parent and its children. So in RecordActivating, I get the new parent (if not on it), and check if any other parent/child records are dirty, and if so, do the update service call on the parent+children as unit of work. In theory, that would leave at most one dirty parent sitting around with careless users.
I dumped the idea of the bubbling dirty flag, because that precludes an edit rollback on the parent if a child set the dirty flag. So I check parent and children for dirty -- easy enough with LINQ and/or an extension method.
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.