Mark for deletion

Posts   
 
    
skreekc
User
Posts: 2
Joined: 20-Mar-2007
# Posted on: 20-Mar-2007 16:33:17   

We are trying to figure out a way to mark an entity for deletion so that it is not deleted until the Save() method is executed on the Entity (instead of deleting itself immediatly from the database). This is for issues of being able to rollback changes, and not having to commit the deletion until the save event.

We added a MarkForDelete() property in our EntityBase we inherit from, and we read that property by overriding the OnSave event. It checks if it is MarkForDelete() = True then it does a Me.Delete().

Our issue is that we are also adding new entities that have the same unique constraints in the database. So if the entity that is new is saved before the old entity is deleted, we will hit the Unique constraint.

The main saves that are used are trickled down to all children using Entity.Save(True). So basically we need a way to make sure the Save process is ran on all entities that are MarkForDelete = True first, so new entities with an identical unique constraint don't have to worry about those still being in the database.

I hope this makes sense haha.

Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 20-Mar-2007 16:44:32   

Hi,

I guess the UnitOfWork feature can help you on this case. Have you seen the Generated code - Unit of work and field data versioning section in the doc ?

Cheers,

Aurélien

skreekc
User
Posts: 2
Joined: 20-Mar-2007
# Posted on: 20-Mar-2007 16:56:49   

We have looked into it but we where hoping to find a more generic solution so that we wouldn't have to setup UnitsOfWork everytime we built an entity structure.

Our ultimate goal was to extrend the base class so that we can simply mark an entity for deletion, and whenver you do the SelfService .Save(True) it can handle the operation in the correct order.

What determines the order of OnSave events when you just do a SelfServicing Save(True) call?

If there is an event handler we could override that would allow us to setup a single UnitOfWork for all entities, so that it adds the entities marked for deletion first, that would be great.

Travis

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 21-Mar-2007 01:38:54   

Unit of work would probably not work out for you here. The deletion always happens last. Now the way I would handle this is to maintain a list of entities to be deleted in a separate collection. And then delete this collection first when saving.

If cannot do this then I would suggest a C# predicate possibly.

So you could do something like this.



            EmployeeCollection employees = new EmployeeCollection();
            // fill employees and mark the appropriate entities for deletion
            List<EmployeeEntity> emps = new List<EmployeeEntity>(employees);
            List<EmployeeEntity> deleteEmps = emps.FindAll(ToDelete);
            EmployeeCollection deleteEmployees = new EmployeeCollection(deleteEmps);
            deleteEmployees.DeleteMulti();

        private static bool ToDelete(ExtendedEntity entity)
        {
            return (entity.MarkForDelete);
        }
        private static bool ToKeep(ExtendedEntity entity)
        {
            return (!entity.MarkForDelete);
        } 


I've never done this, but I think it should work. I would prefer to stay with the save collection and delete collection myself. It seems a bit more straightforward and is easier to bind the entities that will exist because they can be removed from the collection that is to be saved.