detele and recreate child objects

Posts   
 
    
mdissel
User
Posts: 92
Joined: 16-Sep-2003
# Posted on: 15-Dec-2006 15:26:55   

Hello

(using LLBLGen Pro 1.0.2005.1 dotNET 1.1, selfservice)

When the Save method on an Entity called i'm checking if a child collection should be recalculated.. If true i will delete the collection and re-add the new items..

How can i do this inside a single transaction (to prevent deleting without adding and other exceptions)?

Currently i'm override the OnSave, but where should i create a transaction (this.Transaction can be empty)?

override void OnSave(){
this.ChildCollection.DeleteMulti();
// add new Childs
base.OnSave();
}

Thanks

Marco

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 16-Dec-2006 02:12:09   

Create the transaction. Then add the collection to delete and the entity to save then do all your work and commit the transaction. Without knowing much about you code here's an example.


                        TestEntity test = new TestEntity();
            TestCollection col = new TestCollection();
            Transaction txn = new Transaction(IsolationLevel.ReadCommitted, "Test");
            try
            {
                txn.Add(test);  
                txn.Add(col);
                col.GetMulti(null);
                test.TestName = "Test";
                col.DeleteMulti();
                test.Save();
                txn.Commit();
            }
            catch
            {
                txn.Rollback();
            }
            finally
            {
                txn.Dispose();
            }
mdissel
User
Posts: 92
Joined: 16-Sep-2003
# Posted on: 16-Dec-2006 17:05:19   

Thanks for the sample but that's creating an inner transaction...

Pseudo code, somewhere in the UI.


Item item = new Item(100);
item.Name += "a";
a.Save();

When Save() is called (and property Name is changed) i need to recalculate another collection..


override void OnSave(){
this.ChildCollection.DeleteMulti();
for x = 0 to Count{
this.ChildCollection.Add(...);
}
base.OnSave();
}

All these actions inside the OnSave AND the save of this entity should be executed in a transaction..

Why? Suppose Item.Save() fails, i've already refreshed the ChildCollection, resulting in wrong information..

Thanks

Marco

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 17-Dec-2006 02:10:38   

You should be able to use the Transaction that your Item entity is already using. Just use "this.Transaction" inside your OnSave() method.

mdissel
User
Posts: 92
Joined: 16-Sep-2003
# Posted on: 17-Dec-2006 11:55:49   

But the Entity it not always part of a transaction, resulting in this.Transaction is null.

I could check this inside OnSave, but then i should commit the transaction somewhere, but where??

Thanks

Marco

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 17-Dec-2006 19:01:03   

mdissel wrote:

But the Entity it not always part of a transaction, resulting in this.Transaction is null.

You're right - I was thinking of a recursive operation where one is always created.

mdissel wrote:

I could check this inside OnSave, but then i should commit the transaction somewhere, but where??

You should have a higher level business object that handles all of this. The GUI would then call this business object, which would in turn handle instantiating a transaction and your Item entity, and would handle performing the rollbacks.

mdissel
User
Posts: 92
Joined: 16-Sep-2003
# Posted on: 17-Dec-2006 19:50:02   

Hmmm..

And how can i prevent mistakes from developers that are calling this Save method directly, with possible wrong data as a result..?

Thanks

Marco

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 18-Dec-2006 11:46:00   

Selfservicing's Save() method isn't recursive. If a save is recursive, all objects in the tree will be saved and what's better: a transaction will be created for you if it's not there.

So you shouldn't override OnSave() as that's called when the save action is in progress. What you should do is override: Save(IPredicate updateRestriction, bool recurse)

This method is defined in the generated _entityName_EntityBase class. In that override, you add the code for clearing/new item addition to the collection, and IF you needed to to so, you simply pass true for recurse when calling the base method. simple_smile .

I'm not sure if you also wanted to perform the deletes, which I think you want to. If you also want to issue the deletes, copy the code from the base' Save method over to your override and which means you start the transaction IF necessary, locally in your override. As the code in the base method checks if a new transaction is necessary, this will work OK.

Frans Bouma | Lead developer LLBLGen Pro
mdissel
User
Posts: 92
Joined: 16-Sep-2003
# Posted on: 18-Dec-2006 12:51:33   

ah... Thanks for the answer!!

Marco