Making a copy of an entity

Posts   
 
    
slnsalim
User
Posts: 10
Joined: 12-Jan-2004
# Posted on: 02-Feb-2004 02:22:12   

Hi

Here's the scenario: I have an entity, SHOW, that holds several other entity collections, ie SHOW contains SONGS and ARTISTS

I make changes to my entity and its collections via several ASP.NET WebForms, so I persist the changes in memory via the Session object. When I create or modify the entity, I will also update the Session like this:

Session.Remove("SessionName"); Session.Add("SessionName", entity);

but I find that when I change the entity (and don't update the Session object), the changes are reflected in my Session anyway. That's fine, but the problem arises when I save my changes to the database. Saving clears the Collections in the entity hence clearing them from my session as well. This is not an issue if the save succeeds as I can refetch the data. But if the save fails, I lose all my in memory changes. (BTW I wrap the save in a transaction)

In short, I would like to make a copy of the entity which is independent of the original and save that in Session. How do I go about doing this or is there a better way for me to persist the entity and not lose in memory changes if a save fails?

Cheers Sarina

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 02-Feb-2004 09:26:32   

slnsalim wrote:

Hi Here's the scenario: I have an entity, SHOW, that holds several other entity collections, ie SHOW contains SONGS and ARTISTS

I make changes to my entity and its collections via several ASP.NET WebForms, so I persist the changes in memory via the Session object. When I create or modify the entity, I will also update the Session like this:

Session.Remove("SessionName"); Session.Add("SessionName", entity);

but I find that when I change the entity (and don't update the Session object), the changes are reflected in my Session anyway.

Correct, this is because you reference the same object from the session as you do in your code simple_smile

That's fine, but the problem arises when I save my changes to the database. Saving clears the Collections in the entity hence clearing them from my session as well. This is not an issue if the save succeeds as I can refetch the data. But if the save fails, I lose all my in memory changes. (BTW I wrap the save in a transaction)

With the 28-jan-2004 release, I have changed this behaviour. After a save, the entities became out-of-sync, which was a signal for the refetch routine to refetch the entity. This was done when you read a property. The refetch routine cleared all internal collections and removed internal references to related objects. This has changed: refetch no longer clears the collections nor does it remove references to related objects. A lot of users complained about this and it was more harmful than helpful.

In short, I would like to make a copy of the entity which is independent of the original and save that in Session. How do I go about doing this or is there a better way for me to persist the entity and not lose in memory changes if a save fails?

I'd suggest to re-generate your code with the 28-jan-2004 release. This should make the removal of references to related objects go away.

If that's not working for you, let me know.

Frans Bouma | Lead developer LLBLGen Pro
slnsalim
User
Posts: 10
Joined: 12-Jan-2004
# Posted on: 02-Feb-2004 12:47:38   

All is well after installing the new release simple_smile

Thanks very much.