Question about List.Remove(child)

Question about List.Remove(child)

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


Patrick.Roeper posted on Monday, August 27, 2007

I have a root object with a child collection and I am trying to remove an a child element from the child collection. I have been calling the Remove(x) method of the collection, which adds the child 'x' to the deletedlist of the Collection.

Everything up to this point I understand... However, I have 4 levels of nested business objects: Role (Root) -> Tabs (ECL) -> Tab (EC) -> Groups (ECL) -> Group (EC) -> Commands (ECL) -> Command (EC).

My question is: Does calling Remove(tab) on my Tabs child collection automatically mark all groups under tab and all commands under group in the sub-child collections to be deleted as well? I am not seeing the changes trickle down so I am wondering if this needs to be called explicitly.

Thanks in advance.

RockfordLhotka replied on Monday, August 27, 2007

No, the objects don't get marked for deletion. The assumption is that, during the DP_Update() process, the deleted child will delete itself and its children from the database (or it will call a sproc to do that). Those child objects then just vanish from memory and all is well.

If that's somehow not your model then you'd want/need to cascade the delete call through the child object graph yourself, and have each of those objects delete themselves during DP_Update() - but that'd be a lot less efficient.

Patrick.Roeper replied on Wednesday, August 29, 2007

I am open to suggestions on how to accomplish this in the backend by using sprocs... The part that I am caught up on is how the delete can cascade down the object hierarchy. Using the example I put above, if i delete a Role object then the entire hierarchy should be removed from the database. I can tell a role to remove itself from the Roles table where the primary key matches the Role's object value; I can forward the Role Id into the tab's delete sproc and say delete all Tabs where the foreign key to the role matches the specified role Id parameter; after this I need to delete all groups where the foreign key to the tabs table is one of the many tabs that were deleted...

Do you see where I am getting at? I see 2 possibilities for a fix if i am going to do this in the database alone: 1 would be to replicated the foreign keys down the object hierarchy so I can just keep passing in the parameters down the chain. 2 would be to use a cursor through a result set and then call the object delete sproc for each iteration of the cursor.

Both sound like horrible ideas so if someone can give me some advice I would really appreciate it.

mr_lasseter replied on Wednesday, August 29, 2007

If you are using SQL server you could always add a cascade on delete constraint onto your foreign keys.

ReadOnlyChild replied on Wednesday, August 29, 2007

maybe have a sproc like so:

spDeleteRoleHierarchy(@roleId):

DELETE FROM Command WHERE groupId IN (
    SELECT groupId FROM Group WHERE (tabId IN (
        SELECT tabId FROM Tab WHERE (roleId = @roleId) ) ) )

DELETE FROM Group WHERE tabId IN (
    SELECT tabId FROM Tab WHERE (roleId = @roleId) ) )

DELETE FROM Tab WHERE roleId = @roleId

DELETE FROM Role WHERE roleId = @roleId

--

some more to handle the inner levels, or logic on one sproc to delete the appropriate tables only depending on another parameter spDeleteRole(@startAt, @id)

saludos!

Patrick.Roeper replied on Wednesday, August 29, 2007

Its always something simple... Thanks for the help.

Copyright (c) Marimer LLC