Loading an Entity

Posts   
 
    
Slowhand
User
Posts: 96
Joined: 07-Mar-2006
# Posted on: 26-Apr-2006 10:22:35   

Hello, when does LLBL load an entity from the database in the following scenario:


EntityCollection nameColl = new EntityCollection(new NameEntityFactory());
IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(PredicateFactory.CompareValue(NameFieldIndex.id, ComparisonOperator.LesserThan, 100));
bool load = adapter.FetchEntityCollection(nameColl, filter);
//if load ist true, the entity is loaded from the database


//Now ( a little bit later) I would like to load an entity from an related table(class) Adress too.
//I know that I can do this by using the PrefetchPath an the whole thing would look like this:

EntityCollection nameColl = new EntityCollection(new DispoNameFactory());
            IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.NameEntity);
            prefetchPath.Add(NameEntity.PrefetchPathAdress).SubPath.Add(Adress.PrefetchPathnrname); 
            IRelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(PredicateFactory.CompareValue(NameFieldIndex.id, ComparisonOperator.Equal, 1));  
            adapter.FetchEntityCollection(nameColl, filter, prefetchPath);


Does LLBL Gen loads in this second case the nameColl from the database, or does LLBL Gen merge the related and of course new AdressObject to the "old" nameColl" EntityCollection which was filled in the first scenario and loads only the new Adress Object from the database ?

Maybe you can suggest me a better way to do this ?

Thank you, Slowhand

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 26-Apr-2006 10:47:03   

Slowhand wrote:

Hello, when does LLBL load an entity from the database in the following scenario:


EntityCollection nameColl = new EntityCollection(new NameEntityFactory());
IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.PredicateExpression.Add(PredicateFactory.CompareValue(NameFieldIndex.id, ComparisonOperator.LesserThan, 100));
bool load = adapter.FetchEntityCollection(nameColl, filter);
//if load ist true, the entity is loaded from the database


//Now ( a little bit later) I would like to load an entity from an related table(class) Adress too.
//I know that I can do this by using the PrefetchPath an the whole thing would look like this:

EntityCollection nameColl = new EntityCollection(new DispoNameFactory());
            IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.NameEntity);
            prefetchPath.Add(NameEntity.PrefetchPathAdress).SubPath.Add(Adress.PrefetchPathnrname); 
            IRelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(PredicateFactory.CompareValue(NameFieldIndex.id, ComparisonOperator.Equal, 1));  
            adapter.FetchEntityCollection(nameColl, filter, prefetchPath);


Does LLBL Gen loads in this second case the nameColl from the database, or does LLBL Gen merge the related and of course new AdressObject to the "old" nameColl" EntityCollection which was filled in the first scenario and loads only the new Adress Object from the database ?

Maybe you can suggest me a better way to do this ?

Thank you, Slowhand

It loads new instances and doesnt merge. If you want to get the same instances back, you should use a Context object. Add the original nameColl to a context and simply pass that one again (not the new instance of nameColl) to the second fetch.

Frans Bouma | Lead developer LLBLGen Pro
Slowhand
User
Posts: 96
Joined: 07-Mar-2006
# Posted on: 26-Apr-2006 13:17:12   

When I understand your correct, LLBL Gen loads the NameObjects again from the database and makes the relation to the Adresses. Can a context object avoid this ? ( I´m not so familiar with ContextObjects )

Sorry that I ask again,but it´s very imortant for us and for our choice of an adequate O/R mapper.

Slowhand

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 26-Apr-2006 14:37:15   

Slowhand wrote:

When I understand your correct, LLBL Gen loads the NameObjects again from the database and makes the relation to the Adresses. Can a context object avoid this ? ( I´m not so familiar with ContextObjects )

No matter what system is implemented, any o/r mapper will always connect to the db again. The reason is this: say you have 100 customer entities in the DB, and you load 10 in a collection. Say you have a cache which caches these 10. Then you want all customers which match a given filter. The only way the o/r mapper knows exactly which entities to load is to query the DB. It can't fall back onto the cache, because it can be that in the DB there are also entities matching your filter.

So it re-queries the db and the context ensures that you'll get the same entity instances back (uniquing) instead of new ones.

Frans Bouma | Lead developer LLBLGen Pro
Slowhand
User
Posts: 96
Joined: 07-Mar-2006
# Posted on: 26-Apr-2006 14:57:27   

Ok, thank you. I think the performance ist probably the same in both cases ?

Slowhand