Undelete deleted child. [before re-submiting Save()]

Undelete deleted child. [before re-submiting Save()]

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


Dog Ears posted on Saturday, August 12, 2006

I asked this question on the old forum but never really go answered :o(

How should i be handling an edited child collection where a deleted child violates a rule [at the DB during save] that prvents it's deletion.  i don't whant to cancel the whole update as other children may have been edited and the updates need to be kept. 

How can i inform the user that the child item wasn't deleted and return it to the collection in the rolled back graph [undelete it]  Am i missing something?

Many thakns for your input.

Kind regards,
Graeme.

 

 

 

 

RockfordLhotka replied on Saturday, August 12, 2006

I think in such a case you are probably best of reloading the collection from the database.

During the save process, the edit level is 0, so there's no undo possible. If a deleted object can't be deleted, there is no mechanism by which you can get it back into the active list - at least not built into CSLA itself.

Another alternative might exist though, because DeletedList is protected. This means that you could, in your collection, move the object from DeletedList back to the active list. You'd also have to create a method (probably Friend/internal) on the child class so you could force its IsDeleted value back to false. Hmm, and that may not be possible, because I don't think there's a protected method to do that.

So in the end, I think you would probably be best served by simply reloading the collection from the database.

That wouldn't require a Fetch call btw. You are already on the app server when running in DataPortal_Update, and so there's nothing stopping the collection's Update() method from deciding to drop all its child objects and reload them from the database.

Dog Ears replied on Sunday, August 13, 2006

RockfordLhotka:
You are already on the app server when running in DataPortal_Update, and so there's nothing stopping the collection's Update() method from deciding to drop all its child objects and reload them from the database.

That gives me an idea..!
Can't you just reload one item, the item that was incorrectly deleted.   But for simplicity if the user only deleted the object then it it wouldn't really need to be reloaded?

I' raise an error in my child Update() method after calling MarkNew() & MarkOld()

stored proc here retuns a specific result that signifies the failed delete.

Select Case Convert.ToInt32(cmd.GetParameterValue("@Return"))
   
Case 1 'concurrency conflict.
      Throw New Shared.ConcurrencyException
   Case
 2 'delete not allowed.
      MarkNew()
      MarkOld()
      Throw New Shared.ForbiddenDeleteException
....

Ive tried this in my child collection Update() method:

For Each obj In deletedlist
   
Try
      obj.Update(Circuit, transaction)
   Catch ex As Shared.ForbiddenDeleteException
      list.Add(obj)
   
End Try
Next

This seems to work after the user clicks 'apply' the failed deleted child myseriously reappears.   Next it would be nice to inform the user what had happened, any ideas?

Kind regards,

Graeme.

Copyright (c) Marimer LLC