Recursive save when override of SaveEntity

Posts   
 
    
Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 24-Mar-2006 14:57:54   

Hi I've juste switched from 1.0.2004.1 to 1.0.2005.1 version, and something strange happend now : I had an override of SaveEntity method. Previously, it run into this override, recursively for each entity in the graph, but now it only run it only for the 1st object in the graph. This lead me to some problem because my override is used to : - make some audit - automaticaly update some values in an entity - call delete of the entity instead of save in some case

The first 2 points are quite simple to solve : move the code in the "OnSaveEntity" or "OnSaveEntityComplete" override. But the last one is now quite hard and I don't see how to make that ? For example, I can have A -> B -> C and for some reason when I save the graph, instead of saving C I'll delete it. Of course it'll never happend for B, because it'll crash in the db if I try to delete it.

I try to read carefully the change log (when new version are release) to check if some changes can break my code, but I didn't see something about this change of behaviour ?

Thanks for the help

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Mar-2006 15:33:18   

But the last one is now quite hard and I don't see how to make that ? For example, I can have A -> B -> C and for some reason when I save the graph, instead of saving C I'll delete it. Of course it'll never happend for B, because it'll crash in the db if I try to delete it.

Sorry I didn't catch the problem, would you elaborate more. Also it would be very helpful uf you showed a code snippet of what you are trying to do.

Thanks

Fabrice
User
Posts: 180
Joined: 25-May-2004
# Posted on: 24-Mar-2006 15:51:32   

Ok so here is a simplified version of the code

        public override     bool        SaveEntity(IEntity2 entityToSave, bool refetchAfterSave, IPredicateExpression updateRestriction, bool recurse)
        {
            // Some code here for audit

            // Delete the entity if some conditions are filled
            bool deleteEntity = true; // in fact there is some code to determine true or false
            if (deleteEntity) 
            {
                // don't save, but delete it !
                return this.DeleteEntity(entityToSave);
            } 

            // Save the entity
            return base.SaveEntity (entityToSave, refetchAfterSave, updateRestriction, recurse);
        }

In version 2004.1, it work fine because for each entity in the graph it go in my SaveEntity method (override of base method, and the base method call itself recursively). But now, I suppose the base method doesn't call itself recursively ... so I can't change the way the entity is saved, and I can't delete it when needed.

Example : I've an Employee 'myEmployee' which is modified and need to be saved. I've in myEmployee.TimeLineElement several entities which also have to be saved. But in these collection, when saving a element I need (if it meet some conditions) to delete it instead of update it.

Previously I checked these conditions in the SaveEntity. The method was called for each entity by llblgen: SaveEntity(myEmployee, recursive=true) (called by my code) recursively called by llblgen --> SaveEntity(myTimeline1) recursively called by llblgen --> SaveEntity(myTimeline2) recursively called by llblgen --> SaveEntity(myTimeline3) --> some conditions are filled --> DeleteEntity(myTimeline3)

I hope it's more clear now. Thank for your help.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 24-Mar-2006 17:37:10   

That;s indeed not possible anymore, due to the fact that the queue is calculated up front, for performance reasons.

You can do actions in OnBeforeEntitySave(), but you can't change the way the entity is treated. So the save can't be aborted, unless you throw an exception there, but that's not what you want ofcourse.

What you CAN DO is to set the IsDirty flag of the entity to false in OnBeforeEntitySave(), it will then result in an empty update query which is ignored.

I'd leave the entity alone and delete the entity afterwards, by adding the entity to a collection for deletion which are all deleted after the save has been completed.

Frans Bouma | Lead developer LLBLGen Pro