THESE FORUMS ARE NOW FROZEN
Please choose "Forums" from the Main menu of www.entityspaces.net to get to our new forums.

How to get an entity to mark itself as deleted in the save method

rated by 0 users
This post has 11 Replies | 0 Followers

Top 50 Contributor
Posts 47
andieje Posted: 07-01-2009 9:42 AM

Hi

I would like to get my entities to 'clean up' after themselves where possible. Is it possible to override the save method somehow so that if certain properties have certain values the entity marks itself as deleted and then deletes itself?

thanks

Top 10 Contributor
Posts 3,881

Yes, you should be able to examine properties in the save method and call "this.MarkAsDeleted()" by overriding Save in your Custom class. However, make sure you check first to make sure the object isn't already deleted because it will choke if you access properties. Something like this is what I'm thinking.

 

Code:
namespace BusinessObjects
{
    public partial class Employees : esEmployees
    {
        public override void Save()
        {
            if (!this.es.IsDeleted)
            {
                if(this.LastName == "Griffin")
                {
                    this.MarkAsDeleted();
                }
            }

            base.Save();
        }
    }
}
 

Does this help answer your question?

EntitySpaces | Twitter | BLOG | Please honor our Software License

Top 10 Contributor
Posts 1,675
BTW, for performance reasons, calling Save() on an ES collection does not call Save() on each entity. If you override Save for Employees, you will probably want to override Save on EmployeesCollection, as well.

David Neal Parsons
www.entityspaces.net

Top 50 Contributor
Posts 47

I was just going to ask that :)

What does happen in the save method of the collection if it doesn't call the save method on each entity in the collection? I need to know what happens so I can override it correctly.

Many thanks

Top 10 Contributor
Posts 1,675
Generally, you just foreach through the collection in it's overridden Save, do the same check on each entity as you did in the entity's overridden Save, then, outside the loop, call base.Save on the collection.

David Neal Parsons
www.entityspaces.net

Top 50 Contributor
Posts 47

Hi

Sorry to be so stupid but how do I iterate over the collection in the save method? I don't know how the collection is implemented internally. All i can see that might be relevant is getEnumerator. Do i use that?

many thanks

Top 50 Contributor
Posts 47

This is what i have written but i don't know if its correct. The collection class is a collection of Alerts

Public Overrides Sub Save()

Dim alert As Alerts

Dim enumerator As IEnumerator = Me.GetEnumerator

While (enumerator.MoveNext)

alert = CType(enumerator.Current, Alerts)

if (sone condition)

alert.markasdeleted

alert.save()

end if

End While

 

 

 

MyBase.Save()

End Sub

Top 10 Contributor
Posts 1,675

When you click Reply, the right-most icon should be a "Source Code" icon. If you paste your code in the dialog, and select your language, the posted code should be more readable.

Do not call alert.Save() within the loop in your collection override. MyBase.Save() handles them all. If you need to call MarkAsDeleted() within the loop, then iterate the collection backwards to avoid iteration indexing anomalies as entities disappear from the collection.

Code:
Public Overrides Sub Save()
    For position As Integer = Me.Count - 1 To 0 Step -1
        If SomeCondition Then
            Me(position).MarkAsDeleted()
        End If
    Next

    MyBase.Save()
End Sub

If your condition is not too complex, you can use a filter on the collection. The Filter uses standard .NET DataColumn Expression syntax. MarkAllAsDeleted() honors any filter conditions applied. Be sure to remove the Filter before calling MyBase.Save().

Code:
Public Overrides Sub Save()
    Me.Filter = "[Discontinued] = true"
    Me.MarkAllAsDeleted()
    Me.Filter = String.Empty

    MyBase.Save()
End Sub

David Neal Parsons
www.entityspaces.net

Top 10 Contributor
Posts 3,881

As to how do you iterate you should be using For Each but perhaps I didn't read enough info on that ...

EntitySpaces | Twitter | BLOG | Please honor our Software License

Top 10 Contributor
Posts 1,675

Right. I did suggest that earlier, but he wants to call MarkAsDeleted. According to MSDN docs, he should not do that within a For Each. It will do bizarre things like skip every other row.

http://community.entityspaces.net/forums/thread/7264.aspx

David Neal Parsons
www.entityspaces.net

Top 50 Contributor
Posts 47

Hi

sorry to be so paranoid but is it ok to take the entity 'out of the collection' to examine it and to mark it as deleted like so

Code:
  Public Overrides Sub Save()
            For position As Integer = Me.Count - 1 To 0 Step -1
                Dim alert As Alerts = Me(position)

                if some condition
                            alert.MarkAsDeleted()


                End If
                    
            Next

            MyBase.Save()

        End Sub

My processing to determine whether the entity should be deleted is reasonably long so its a lot easier like this. The alert variable is a reference to the same entity within the collection so it should be ok shouldnt it?

thansk

Top 10 Contributor
Posts 1,675

Since the "alert" entity is still part of the collection, that looks OK, but only thorough testing will ease your paranoia. Wink

David Neal Parsons
www.entityspaces.net

Page 1 of 1 (12 items) | RSS
Copyright © 2005 - 2009, EntitySpaces, LLC