Delete

Delete

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


KJosh posted on Wednesday, October 22, 2008

Hi,

I have the classes structure like the below:

Parent : BusinessBase

     ChildList : BusinessListBase<childLits, Child>

          Child : BusinessBase

In the parent, included the childList as the property. The ChildList  has the remove method which will remove the child from the childlist collection. Then from the Parent's DataPortal_DeleteSelf, how we delete the child objects which are in childlist collection based on the childId?

Parent class:

Private override void DataPortal_DeleteSelf()

{

   Here need to do like for each child in deletedList,  How to call the child's DeleteSelf method?

}

from the UI: Parent.ChildList.Remove(child1);

   parent.Save() -> This will call the Parent's DeleteSelf method. Then How to achieve the above?


   

tetranz replied on Wednesday, October 22, 2008

I think you are asking what happens when you delete the parent which means, of course, that all children must be deleted. The answer is to do it directly on the database with your stored procedure or Linq or whatever. i.e, delete the child records from the database and then the parent record. You don't need to do anything with the child objects. See the ProjectTracker example. It covers must common scenarios.

KJosh replied on Wednesday, October 22, 2008

No, I do not want to delete from the parent in the stored procedure. I want to delete the selected child items from the childlist not all? Thatswhy from the parent, I need to call the Child_Deleteself method by passing the childId like

foreach(child c in deletedlist)

{

c.delete_self(c.Id);

}

I need to do the above for loop in the Parent's deleteself method. But in that method deletedlist not available? How to achieve this?

tetranz replied on Wednesday, October 22, 2008

KJosh:

from the UI: Parent.ChildList.Remove(child1);

   parent.Save() -> This will call the Parent's DeleteSelf method. Then How to achieve the above?

No, if you remove a child then parent.Save() will call the parent's DataPortal_Update, not the parent's DeleteSelf. It will only call the parent's DeleteSelf if you actually delete the parent.

Again, I suggest that you look at the ProjectTracker example. See how Project.cs calls DataPortal.UpdateChild( ... ). That will call the child's Child_Insert, Child_Update or Child_DeleteSelf as required. CSLA does all the looping and "magic" for you.

Project tracker doesn't show it but you can reduce the code slightly more. Instead of DataPortal.UpdateChild ( ... ) you can simply call FieldManager.UpdateChildren( ... ). That does the same thing except that it will do all child collections if you have more than one. The parameters are a parameter array so you can pass anything. Those parameters are passed to the Child_Insert, Child_Update or Child_DeleteSelf so it is very flexible. Very cool Smile [:)]

Copyright (c) Marimer LLC