Delete root

Delete root

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


Slayer posted on Thursday, November 27, 2008

Hi

Im went through the csla 2.0 sample project. I have a question. I fetch a project object. The object contains a collection of resources. I then delete the project object. What happens to the objects status? The object is in memory even though it has been delete. What happens if the save method is executed again while the object and its children are no more ?

Thanks

rsbaker0 replied on Thursday, November 27, 2008

I haven't looked at the sample code lately, but in our application the object is marked "old" after the delete is committed to the database. A second call to Save() would have no effect since the object does not appear dirty. If you did change a field and make the deleted object dirty, the database update would fail (similar to a concurrency violation) because the record no longer exists in the database.

However, this is because I implemented the save functionality myself and this is what I chose to do. CSLA is fairly agnostic as to what happens during object saving, so to some degree it is can be up to you to implement your own behavior in this case.

JoeFallon1 replied on Saturday, November 29, 2008

You should be able to find this out by:

1. Running the code and stepping in to CSLA to see what it does.

2. Reading the book. (You did buy a copy, right?) You can get the 2008 book (a preview edition is available as .pdf if you pre-order it.)

As I recall the DataPortal marks your BO as New once the delete occurs because the data is no longer in the database. Thus another save should cause an Insert to occur.

Also, keep in mind that the Delete of a Root needs to handle the Delete for all its children in code (unless you have Cascade Delete turned on int he database.)

Oh and avoid the #1 bug of UI code saving BOs:

Do NOT do this:

myBO.Save

Do this instead:

myBO = myBO.Save

You have to update the variable with the returned object.

Joe

 

ajj3085 replied on Monday, December 01, 2008

How are you deleting the object?  Are you calling the Delete method on the instance?  Or using a static method to delete it? 

If you're calling Delete on the instance, the object is marked to be deleted.. but not deleted.  To commit the delete, you need to call Save, which will mark the object as Dirty and New.  I would think you'd mimic that behavior if you have a Delete static method as well.  Of course all the child objects would have to be marked as such as well.

If you were then to call Save again, I would think it would re-insert all the data, although with new keys (possibly.. it depends on what data types you're using as key columns).

Check out the book for more details, and why this behavior makes sense.

HTH
Andy

Copyright (c) Marimer LLC