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
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.
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
David Neal Parsonswww.entityspaces.net
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
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
This is what i have written but i don't know if its correct. The collection class is a collection of Alerts
Public
alert =
alert.markasdeleted
alert.save()
end if
End While
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.
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().
Public Overrides Sub Save() Me.Filter = "[Discontinued] = true" Me.MarkAllAsDeleted() Me.Filter = String.Empty MyBase.Save() End Sub
As to how do you iterate you should be using For Each but perhaps I didn't read enough info on that ...
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
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
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
Since the "alert" entity is still part of the collection, that looks OK, but only thorough testing will ease your paranoia.