How to delete grandchild?

How to delete grandchild?

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


tchimev posted on Thursday, September 25, 2008

I have design my business objects like this:
BLB parent
-------BB child
------------BB grandchild
------------BB grandchild

When I call BLB.Remove(index), my BB child is marked for deletion and on BLB.Save() it is deleted, but my grandchildren are not marked for deletion.
How shoud I mark my grandchild for deletion?

triplea replied on Thursday, September 25, 2008

Looking at the PTracker sample app you can see a different approach. There you have:

BB (Project)
------BLB (Resources)
------------BB (Resource)

When a Project is marked for deletion, you are not deleting all the children. The idea (if I remember well) is that when you delete an "entity" from your database, it is better to put all the logic for marking all the related child records for deletion (or physically deleting them if you really want). There is no real need to mark the children (all Resource in Resouces) as deleted.

Saying that, if you really want to mark all your child objects as deleted I guess one way is to override Delete(). Not sure this will work in your scenario (since your root is a BLB) but the idea would be to mark all your child objects for deletion manually, the same way that in CSLA 3.0.x you have to override IsValid and IsDirty.

Hope this makes sense.

tchimev replied on Thursday, September 25, 2008

If I understand right,
one way is to delete my grandchildren physically in the Child_DeleteSelf() method of my child object or
override my grandchild Delete() method, so I can mark it for deletion in the Child_DeleteSelf() method of my child object.

But if I do not want my child to physically delete his children and
I know it is a bad practice to call Delete() directly on the child object, are there other ways to delete grandchildren?

If someone else has something to add to this thread, I'll be grateful.

JoeFallon1 replied on Thursday, September 25, 2008

Here is a sample region for my DataPortal_Delete code. It includes hooks for deleting children. (And anything else that needs to be deleted too. Like messages, misc linked records, etc.) There is some non-CSLA code here but I have shared most of it before. You should be able to get the whole idea from reading this code.

===================================================================

Protected Overrides Sub DataPortal_DeleteSelf()
  DataPortal_Delete(
New CriteriaCode(GetType(SomeBO), "DataPortal_DeleteSelf", mID))
End Sub

Protected Overrides Sub DataPortal_Delete(ByVal criteria As Object)
 
Dim user As MyUser
  user =
DirectCast(Thread.CurrentPrincipal, MyUser)

 
Dim crit As CriteriaCode = DirectCast(criteria, CriteriaCode)
  Dim strSQL As String
= MyDAO.Delete(crit.Code)
 
Dim tr As IDbTransaction = Nothing

  Try
   
tr = Me.BeginTransaction()
   
'Delete child objects before deleting the parent.
   
DeleteChildren(tr, criteria)
   
'delete the parent
   
DAL.ExecuteNonQuery(tr, CommandType.Text, strSQL)
    PostDeleteData(tr, user)
   
Me.EndTransaction(tr)
 
Catch ex As Exception
   
Me.RollbackTransaction(tr)
   
Throw
 
End Try
End Sub

Protected Overridable Sub DeleteChildren(ByVal tr As IDbTransaction, ByVal criteria As Object)
 
'marker method that can be overridden in derived class
End Sub

Protected Overridable Sub PostDeleteData(ByVal tr As IDbTransaction, ByVal user As MyUser)
 
'marker method that can be overridden in derived class
End Sub

===================================================================

Joe

Copyright (c) Marimer LLC