Centralized recursive deletes

Posts   
 
    
Roman
User
Posts: 23
Joined: 19-May-2006
# Posted on: 18-Apr-2007 23:01:04   

I understand that LLBLGen does not recursively delete related entities and it is up to the developer to take advantage of AddDeleteEntitiesDirectlyCall for recursive entity deletes. I have gone the route of creating methods such as this:

public void DeleteSomeEntity(int entityID) { SomeEntity entity = new SomeEntity(entityID); UnitOfWork2 uow = new UnitOfWork2();

// Delete children of SomeEntity
uow.AddDeleteEntitiesDirectlyCall("ChildEntity", entity.GetRelationInfoChild());

// Delete SomeEntity
uow.AddDeleteEntitiesDirectlyCall("SomeEntity", new RelationPredicateBucket(SomeEntityFields.ID == entityID));

// Delete potential orphan records that SomeEntity referred to
// The predicate should be based on some info that was retrieved prior to SomeEntity's deletion
uow.AddDeleteEntitiesDirectlyCall("OrphanedEntity", SomePredicate);

uow.Commit(DataAdapter, true);

}

As you can see, the recursive delete is transaction, with additional actions happening before and after deleting the actual desired record. This function is easy to call from, say, a button click, but what about when LLBLGenProDataSource2 is processing SomeEntity deletes from a grid? What is the best way to instruct the data source to use my delete method? Currently I would add the following to the PerformWork event (prior to the commit):

foreach (UnitOfWorkElement2 element in e.Uow.GetEntityElementsToDelete()) { SomeEntity entity = element.Entity as SomeEntity; e.Uow.RemoveFromUoW(entity); DeleteSomeEntity(entity.ID); }

Is there some centralized way to wire up and always run the specified recursive delete method when an entity needs to be deleted, without having to intercept the GetEntityElementsToDelete() in the data source event?

Hope this makes sense - thanks!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Apr-2007 09:54:52   

Is there some centralized way to wire up and always run the specified recursive delete method when an entity needs to be deleted, without having to intercept the GetEntityElementsToDelete() in the data source event?

You'd have to call the GetEntityElementsToDelete() method in PerformWork event, just as you did.

I'd have suggested to have a function that accepts a reference to the UOW object taken from the datasource and append the needed entity deletions within.

public void AppendToUOW(UnitOfWork2 uow)
{
  foreach (UnitOfWorkElement2 element in uow.GetEntityElementsToDelete())
 { 
    SomeEntity entity = element.Entity as SomeEntity;
        
    // Delete children of SomeEntity
    uow.AddDeleteEntitiesDirectlyCall("ChildEntity", entity.GetRelationInfoChild());
        
    // Delete potential orphan records that SomeEntity referred to
    // The predicate should be based on some info that was retrieved prior to SomeEntity's deletion
    uow.AddDeleteEntitiesDirectlyCall("OrphanedEntity", SomePredicate);
  } 
}

Then call this method from the PerformWork before commiting the UOW.