Context Object confusion

Posts   
 
    
zulu
User
Posts: 50
Joined: 25-Aug-2008
# Posted on: 04-Nov-2009 23:30:45   

Hello, in the LLBLGen manual, under the Context object section it says:

Context objects have to be created by the developer and live as long as the developer wants and keep objects in their cache as long as the Context objects live. A developer can create multiple Context objects to create different semantic contects in which entity objects are unique. This can help when the developer doesn't want two screens with the same object listed to assure that editing the entity on one screen doesn't automatically alter the other instance as well (because the user can click Cancel for example).

It's not clear, also from the rest of the documentation, how I would ago about achieving what is mentioned above.

To explain better, I have say a CustomerCollection displayed in a screen. Now I want to edit one of those customers, but not the original entity, I want to edit a clone so that if changes are not committed the original instance is not touched.

According to the paragraph above the context object can be used to solve such a problem, but I am not able to figure out how. Any help?

Thanks Simone

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Nov-2009 04:06:53   

zulu wrote:

According to the paragraph above the context object can be used to solve such a problem, but I am not able to figure out how. Any help?

Yes, you can create two separate Context objects for that. But I'm not sure what is your problem or what you don't understand 100%.

David Elizondo | LLBLGen Support Team
rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 05-Nov-2009 15:05:14   

Hi Simone -

New Entity Graphs

There are two ways to obtain a new Entity Graph, either:

1.) Clone the entity before editing. Just do a search for cloning on the forums here. However, be aware that if you simply serialize/deserialize the entity - you will clone the entire graph - meaning all related entities will be cloned, too. Perhaps that's what you want, perhaps not.

2.) Refetch the entity. This is the simplest. Make sure you refetch without a Context or you won't get a new Entity Graph. smile

Context's Purpose

Context is simple - it is simply used to maintain a single entity instance for a single database record across multiple fetches. If you fetch an entity multiple times (yes, sometimes it is necessary) - the Context will prevent two entities being created with the same Primary Key. While fetching - if there is an entity instance with that Primary Key already in memory - it will continue to use that single entity instance.

You can think of it as an Entity Graph helper - used when fetching the same entity multiple times. (We rarely need to use the Context, by the way.)

Hope this helps!

Ryan

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 05-Nov-2009 21:25:27   

As ever with Ryan - what he said!

Matt

zulu
User
Posts: 50
Joined: 25-Aug-2008
# Posted on: 07-Nov-2009 11:01:15   

Hy Ryan, thanks for your time. I did indeed understand that context is used to mantain a single entity instance across multiple fetches.

That's why the documentation was confusing, where it says that context could be used to solve a problem like the one I described. In reality it doesn't, and like you said Cloning or Refetching are the only options, which I've used before and I guess will have to use again.

To be more precise, yes, if you use two (2) Contexts then you can achieve the result I am looking for, but in my case I don't see any difference from simply fetching the entity twice.

Thanks a lot Simone