Deleting children with factory object

Deleting children with factory object

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


Mark Sulkowski posted on Monday, May 09, 2011

As I understand it, any child objects marked for deletion are put in a collection called DeleteList in the child's list object.  This apparently has usefulness for undo functionality, but it also seems handy for containing objects that have recently been deleted and must be deleted from the database after a call to Save().

When using a Data Access layer with factory classes that takes care of such database functions as Update(), Insert(), and Delete(), a call on the Save() method of a root object would call Update() on that root object's data access factory object, and then cascade the updates by calling the Save() method on every child list object owned by the root object, which would then call the Update() method on the list object's factory object, which could then take care of any further updates, insertions, and deletions.

I notice that the DeleteList collection is protected, which means that any data access layer factory object can't get to the list directly.  So, I am wondering how I may best take care of any deletions.

Should I override the business list object's Save() method to iterate through both the DeletedList and active child list objects and call the appropriate methods on those child objects, and then clear the DeletedList?  Or should an Update() method on the data access factory object for the list object take care of calling the delete methods on every child in the DeletedList collection (but how, since that is protected, and how could I clear it)?  Or have I misunderstood something and there is there some better approach?

 

Mark Sulkowski

Linköping, Sweden

JonnyBee replied on Monday, May 09, 2011

Your root object factory classes should cascade the save directly to the factory classes for it's child lists/objects.

the ObjectFactory base class exposes access to internal/protected methods like:

The ObjectFactory attribute is only used in root DataPortal (not Child DataPortal methods), ie:

are the only calls that will use the ObjectFactoryAttribute.

You are not allowed to call Save on an object that is marked as Child -  so your root object factory should call the object factpries for its child lists/objects that in turn should cacade down to grandchildren etc.

The lists objct factory should:

  1. Process the deleted list and call the item object factory Delete method
  2. Clear the deleted list
  3. Process the list items and if child is dirty  call item object factory Insert/Update method

Copyright (c) Marimer LLC