DoNotPerformAddIfPresent & PrefetchPath

Posts   
 
    
nunodg
User
Posts: 14
Joined: 14-Dec-2005
# Posted on: 14-Dec-2005 02:24:00   

Hi all,

I have a problem fetching a collection with 1:n relation.

Example:

EntityCollection customers = new EntityCollection(new CustomerEntityFactory());
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.CustomerEntity);
prefetchPath.Add(CustomerEntity.PrefetchPathOrder);
adapter.FetchEntityCollection(customers, null, prefetchPath);

If the Order table have a large number of records, the fetch is very slow because the Order collection of each Customer is created with DoNotPerformAddIfPresent = true (default value) in the first access to Order Property in SetRelatedEntityProperty.

case "Order":
    this.Order.Add((Order)entity);
    break;

Is possible to improve performance? How can I set DoNotPerformAddIfPresent = false for each Order collection created in Order property?

Thanks

Nuno

Paul.Lewis
User
Posts: 147
Joined: 22-Aug-2005
# Posted on: 14-Dec-2005 04:58:31   

nunodg wrote:

Is possible to improve performance? How can I set DoNotPerformAddIfPresent = false for each Order collection created in Order property?

Nuno

Nuno,

Here's an excerpt from the LLBLGen User Manual.

How do I speed up manual bulk additions to EntityCollection objects using Adapter? If you manually add entities to an EntityCollection object, using code generated with Adapter templates, you'll run into a performance penalty as the Add() method will check wether the entity added is already in the collection or not. This can lead to severe slowness in an application if the collection already holds a lot of entity objects (Add() uses List.Contains() which is a .NET method and which performs a linear search). To prevent this performance penalty, for example if you don't care if an entity object is added twice, you can set the EntityCollection property DoNotPerformAddIfPresent to false. When you do that, all Add() calls will skip checking if an entity is already in the collection and will be significant faster. FetchEntityCollection() always sets this property to false before fetching new entities in the collection. On SelfServicing no such property exist.

So, using your example:

EntityCollection customers = new EntityCollection(new CustomerEntityFactory());
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.CustomerEntity);
prefetchPath.Add(CustomerEntity.PrefetchPathOrder);
customers.DoNotPerformAddIfPresent = false;
adapter.FetchEntityCollection(customers, null, prefetchPath);

Should get you where you want to be.

nunodg
User
Posts: 14
Joined: 14-Dec-2005
# Posted on: 14-Dec-2005 12:14:18   

Paul,

Thanks for the reply.

The problem isn't in the Customer collection but in the **Order **collection of each customer entity. This collection are created in LLBLGen Pro code.

This is a 1:n relation

Example:

If there are 3 Customers, 3 Order collection are created by the FetchEntityCollection (inside SetRelatedEntityProperty method). After that, the FetchEntityCollection fills each Order Collection with records. The **Order.DoNotPerformAddIfPresent is true **and you can only change after data is added.

Any help?

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-Dec-2005 15:02:54   

We will be looking at this one.

But as a fast solution, you may use an include template which already creates the collections for the 1:n relations and then sets the property DoNotPerformAddIfPresent to false.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 15-Dec-2005 12:40:29   

Bind the following to the ID: Custom_EntityInitializationTemplate

(see 'adding your own code to the generated classes')


<[Foreach RelatedEntity OneToMany CrLf]><[If Not MappedFieldRelationIsHidden]>      _<[CaseCamel MappedFieldNameRelation]> = new EntityCollection(new <[RelatedEntityName]>EntityFactory());
        _<[CaseCamel MappedFieldNameRelation]>.DoNotPerformAddIfPresent=false;<[EndIf]><[NextForeach]>

This will generate into each entity initialization code which will set the embedded collections' DoNotPerformAddIfPresent to false. Please let me know if you need further assistence with this.

Frans Bouma | Lead developer LLBLGen Pro