UnitOfWork2 - How to Achieve a Double Save

Posts   
 
    
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 02-Nov-2005 16:55:23   

Frans,

I'm stuck on something which I thought I would ask you about:

I have a entity which has a circular reference with another entity (they both have foreign key fields which point to each other). This circular reference is required for performance reasons and cannot be eliminated...

The problem is when I try to create the entity graph to save under a UnitOfWork2... Obviously the trick is to use a combination of Weak Relations and a double save in order to get all the Identity columns to sync up... BUT... UnitOfWork2 doesn't allow you to add an entity to the save collection twice... disappointed

So I tried to sub class it... but and that's not going to work either as I even if I override AddForSave I can't get access to the private _entitiesToSave collection... disappointed

The other issue is that this all has to happen within a complex framework where the Adapter / UnitOfWork is created and passed in from an upper layer.

The only thing I can come up with is to start a transaction on the adapter and manually save the entity to the db with a refetch (outside of the UnitOfWork). I can then add the problem related entities and add it to the UnitOfWork for a second save. I must keep the transaction open for the remainder of the processing until the upper layer calls commit on the UnitOfWork which will save the entity again. Since UnitOfWork commit uses the same adapter on which the transaction was started earlier.. a failure should rollback both saves...

In theory... confused

What do you thing?

Marcus

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 02-Nov-2005 16:55:50   

I've since implemented this and it works ok... smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 02-Nov-2005 17:49:15   

Marcus wrote:

I've since implemented this and it works ok... smile

You added code to the Uow or implemented it in your own code?

Cyclic references are always a pain. No real solution is sufficient...

Frans Bouma | Lead developer LLBLGen Pro
Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 02-Nov-2005 18:06:56   

Otis wrote:

You added code to the Uow or implemented it in your own code?

No I didn't touch the UoW because the circular reference only causes a problem in 1 method of the application - the CreateAccount method. I just start a transaction which is outside the UoW at the root layer of the app and this catches everything in between (including the double save).