Let's say I have the following classes: person, widget, foo_bars.
A person can have multiple widgets and foo_bars.
When an admin deletes a person, I want my code to do a cascading delete... and remove all related records in the database for this person In my widget class, I have a delete method... and the same is true for my foo_bars class
But in order to accomodate something like the person delete function, I feel like I should be creating "Person Manager" class, that will actually repeat a version all the logic inside both the delete methods in the widgets / foo_bars classes. (instead of just calling the existing methods on the classes.
Here's what I'm thinking:
Example 1
public class PersonManager{
delete_person(personID as int)
{
// assume that I've queried for list of all widget and foobars IDs.
//1. delete all widgets for personID
success = true
dim obj_widget = new widget;
loop through all widgets
result = obj_widget.delete(widgetID)
if not result then
success = false
exit with error
end if
end loop
//2. delete all foo bars for personID
if success then
dim objfoobar= new foo_bars;
loop through all foobars
result = objfoobar.delete(foobarid)
if not result then
success = false
exit loop
end
end loop
end
//3. delete person record.
if success then
result = databaseobj.deleteperson(personID)
if not result then
return false, "Delete person failed because...."
else
return true
end
end
}
}
In the above example, I'm trying to demo the reuse of existing the existing delete methods on each class... and then if it successfully deletes all widgets and foobars for the person, then i delete the person record itself.
The problem with this is that if the widgets delete properly, but the foo_bars don't, how do I rollback? Each delete method has it's own transaction defined within, because deleting a widget or foo bar involves, let's say 2 - 3 steps. So, in other words, the delete method on the widget class currently looks like this:
public class widget{
delete_widget{
database.execute("BEGIN TRANSACTION")
database.execute("DELETE FROM XYX ...")
if successful then
database.execute("DELETE FROM yyy...")
else
database.execute("ROLL BACK")
return false
end if
database.execute("COMMIT")
return true
}
}
For each widget, I'll be calling this method... So what if all the widgets delete, but it dies deleting the first foobar? I think I have to rewrite the code to delete everything right inside the Person manager class and wrap it in a transaction, instead of calling the existing delete methods on the classes. At least, that's what I'm thinking. If there's another way to look at this problem, please let me know. I could just write a cascading delete SQL statement in the PersonManager class, and not call the methods on the two classes. But I don't know if using SQL cascading deletes is a good thing... Thanks!