IsDeleted in EditableRootObject

IsDeleted in EditableRootObject

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


glenntoy posted on Tuesday, October 10, 2006

I have an editable root object, and I try to delete it with the following statements:

Dim myObject as MyRootObject =  MyRootObject.GetMyRootObject(...)
:
:
' after myObject.Delete(), myObject.IsDeleted was set to true
myObject.Delete()

' after the statement below myObject.IsDeleted was set to false and IsDirty  = true?
myObject = myObject.Save()

Is this a normal behavior or I'm missing something? What would be the proper way of doing it using Save() or the Shared Factory method? Just got a little confuse :-)


ajj3085 replied on Wednesday, October 11, 2006

In this case, Delete sets IsDeleted to true, but doesn't do anything else.  Its by design, as you can 'undelete' the object.

When you call save, your DP_U should be called where you delete the data from the database.  After this is completed, MarkNew is called.  New indicates the object exists only in memory and not in the database.  Its no longer marked as deleted (because you can delete something which does not exist) and its Dirty, because in Csla new objects are dirty (although you can override that behavior if you want).

The shared factory method Delete calls DP_D which deletes the item immediately. 

Which one you use depends on what behavor you want.

HTH
Andy

glenntoy replied on Thursday, October 12, 2006

Thanks Andy. Actually, I'm developing an ASP.NET application such that I used a FormView to edit/delete/create an entry.

I wanted a behavior where in the user clicks delete, it will still show the entry that I deleted (which I already achieve because I still have it in memory but not in database), but I also wanted to disable/hide the edit & delete link buttons in a form view when the its on Readonly mode and I was thinking to do this one using LinkButton.Visible = myObject.IsDeleted

I guess I need to override the Save(..), such that it will set the object as deleted. And I think I should be careful on this one. :-)

Public Overrides Function Save() As myObject
       
        'retrieve first the original value of IsDeleted before performing the action
        Dim deleted As Boolean = Me.IsDeleted

        Dim t As myObject = MyBase.Save()

        'only marked as deleted if the previous state was deleted
        If deleted Then t.MarkDeleted()
        Return t
End Function

ajj3085 replied on Thursday, October 12, 2006

Hi,

There's no need to override save to do this.  The Delete method on the instance will mark it as deleted.

In your DataPortal_Update  you'll want to call MarkNew, because the object is 'new' in that it no longer exists in the database.

MarkDeleted is used to tell the object that when Save is called, it should delete itself, NOT to indicate that the object WAS deleted from the database.

IsNew tells you if the object exists in the database (in which case IsNew will be false) or exists only in memory (IsNew will be true).

HTH
Andy

RockfordLhotka replied on Thursday, October 12, 2006

glenntoy:
I have an editable root object, and I try to delete it with the following statements:

Dim myObject as MyRootObject =  MyRootObject.GetMyRootObject(...)
:
:
' after myObject.Delete(), myObject.IsDeleted was set to true
myObject.Delete()

' after the statement below myObject.IsDeleted was set to false and IsDirty  = true?
myObject = myObject.Save()

Is this a normal behavior or I'm missing something? What would be the proper way of doing it using Save() or the Shared Factory method? Just got a little confuse :-)


This is normal. The reason being, that the object WAS deleted by the Save() call, but then you get back a new instance of the object.

By definition, that new object's key value does not match a row of data (because you just deleted it) and so IsNew should be true.

By definition, that new object's fields don't match data in the database, and so IsDirty is true.

And that new object is not marked for deletion - it represents the now-deleted data, but since the delete has already completed, so the object isn't marked to re-delete the already deleted data.

Copyright (c) Marimer LLC