Prefetch paths and inheritance

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 13-Jul-2006 00:59:56   

I have fetches in my code that use common 'prefetches'. So, I simply add a method like:

public static void PrefetchPath_AddFullEntity(IPrefetchPath prefetchPath)
{
    prefetchPath.Add(ThisEntity.PrefetchPathSomeRelationShip);
    prefetchPath.Add(ThisEntity.PrefetchPathSomeRelationShip2);
    prefetchPath.Add(ThisEntity.PrefetchPathSomeRelationShip3);
}

The code to call it:

PrefetchPath prefetchPath = new PrefetchPath((int)EntityType.ThisEntity);
ThisEntity.PrefetchPath_AddFullEntity(prefetchPath);
thisEntity.FetchUsingUCSomeUniqueConstraint(value, prefetchPath);

Note, I do not create the PrefetchPath, but rather add to it. This enables you to combine them together. For example if you had an Entity that contained the ThisEntity above, you could do a full fetch on it by calling:

PrefetchPath prefetchPath = new PrefetchPath((int)EntityType.ContainingEntity);
ThisEntity.PrefetchPath_AddFullEntity(prefetchPath.Add(ContainingEntity.PrefetchPathThisEntity).SubPath);
containingEntity.FetchUsingUCSomeUniqueConstraint(value, prefetchPath);

Works fine. (If you have a better suggestion on how to do this, let me know).

The question/problem is when I have a target per entity hierarchy. If I have EntityB that inherits from EntityA and they have some or all prefetch relationships the same, I end up writing duplicate code. (Note the NEW keyword on the second method, since it inherited the first one). I am assuming this duplicate code is necessary - because if I used the prefetch path from EntityA for an EntityB - it would use the wrong tables.

public static void PrefetchPath_AddFullEntity(IPrefetchPath prefetchPath)
{
    prefetchPath.Add(EntityA.PrefetchPathSomeRelationShip);
    prefetchPath.Add(EntityA.PrefetchPathSomeRelationShip2);
    prefetchPath.Add(EntityA.PrefetchPathSomeRelationShip3);
}

new public static void PrefetchPath_AddFullEntity(IPrefetchPath prefetchPath)
{
    prefetchPath.Add(EntityB.PrefetchPathSomeRelationShip);
    prefetchPath.Add(EntityB.PrefetchPathSomeRelationShip2);
    prefetchPath.Add(EntityB.PrefetchPathSomeRelationShip3);
}

Do you have any ideas/suggestions/workarounds so I do not have to do this - or is this just the best way to do this.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Jul-2006 07:56:06   

I guess you may pass the entityType as a parameter, to end up with one function.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 13-Jul-2006 18:09:06   

Well, yes, I could do that. I just was wondering if there was an alternative or anything different I should be doing when it comes to inherited entities....

I thought there might be a better way, since the prefetch path already knows what inheritance entity we are referring to, would be nice to just specify the relationship (which is the same among all the inherited objects)...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 14-Jul-2006 12:28:09   

If you have A, B, C and D and A has a relation with B and that C and D are subtypes of B, you can use the prefetch path A - B to fetch B, C and D.

Frans Bouma | Lead developer LLBLGen Pro