Context as Cache

Posts   
 
    
arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 10-Feb-2006 01:02:12   

If I'm understanding Contexts correctly. They don't prevent database fetches on entities that are already fetched. Instead they fetch the entity, add it, if it is not in the context. If it is, they update the nondirty entities with newer data and ignore newer data for dirty entities.

If I have the primary key of a (potential) entity is there a good, builtin, way to get the entity from the context if it is there and fetch it if it's not?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 10-Feb-2006 03:45:13   

I don't believe there is any built in functionality for this behavior. The Context does not fetch any data from the database. So if you have the primary key of a potential entity and you request it from the Context you will need to check if it IsNew to determine if has been fetched from the database and stored in the Context or not found in the context and returned as a new entity. In which case you would most likely want to fetch the entity from the database and store it in the Context.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 10-Feb-2006 08:00:34   

It will fetch it in selfservicing due to the PK set action, it wont in adapter. Please compare the two pieces of text under single entity fetches in the docs in selfservicing and adapter.

The context is meant to have a single entity INSTANCE with the same data for a given semantical context. So best is you fetch and use the context to grab the unique instance in the context, OR first try if it's in the context, if not, fetch it, as described in the documentation.

Frans Bouma | Lead developer LLBLGen Pro
arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 10-Feb-2006 13:13:17   

If I have the primary key of a (potential) entity is there a good, builtin, way to get the entity from the context if it is there and fetch it if it's not?

OR first try if it's in the context, if not, fetch it, as described in the documentation.

Yes, Is get the proper method for this? It seems I do this by: 1) entityvariable = context.Get(new Entity(PK)); 2) if(entityvariable.IsNew){ adapter.FetchEntity(entityvariable);}

?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 11-Feb-2006 01:44:53   

I was going more towards a SelfServicing setup. For Adapter you should fetch the entity first and then try to access it from the context. If it already exists in the context then you will receive that version, if it does not that during this get it will be added to the context.

// C# CustomerEntity c = new CustomerEntity("CHOPS"); adapter.FetchEntity(c); c = (CustomerEntity)myContext.Get(c);

arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 11-Feb-2006 14:37:33   

For Adapter you should fetch the entity first and then try to access it from the context. If it already exists in the context then you will receive that version, if it does not that during this get it will be added to the context.

It would be hard to call that a cache! wink

I'm trying to avoid additional database talk for entity graphs I already have. I guess I'll have to set up my own.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 11-Feb-2006 15:47:49   

It's not meant to be a cache simple_smile . You can use it as a small cache, for example by fist consulting the context, then load the entity manually if the context doesn't have it.

The reason it's not a cache is that caching is something you should only do with non-versatile data. Any data that's versatile makes caching problematic: if you fetch a set of entities of type X, you always have to fetch them from the db first: to update already fetched entities in the cache and the ones not in the cache also have to be loaded. The thing is: you don't know which entities in the set to load aren't in the cache, you only know that when consulting the db.

Frans Bouma | Lead developer LLBLGen Pro
arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 11-Feb-2006 16:36:29   

You can use it as a small cache, for example by fist consulting the context, then load the entity manually if the context doesn't have it.

As I said, that's what I wish to do. So to repeat my question:

Yes, Is context.get the proper method for this? It seems I do this by: 1) entityvariable = context.Get(new Entity(PK)); 2) if(entityvariable.IsNew){ adapter.FetchEntity(entityvariable);}

So that if the newly created entity remained new after the context.get I would know I needed to fetch it?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 11-Feb-2006 16:57:26   

arschr wrote:

You can use it as a small cache, for example by fist consulting the context, then load the entity manually if the context doesn't have it.

As I said, that's what I wish to do. So to repeat my question:

Yes, Is context.get the proper method for this? It seems I do this by: 1) entityvariable = context.Get(new Entity(PK)); 2) if(entityvariable.IsNew){ adapter.FetchEntity(entityvariable);}

So that if the newly created entity remained new after the context.get I would know I needed to fetch it?

Sorry that I didn't understood you earlier. simple_smile .

Yes, if the entity isnew, it's not in the context, so you have to fetch it again. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 11-Feb-2006 17:45:21   

Thanks.