Getting a strongly typed PrefetchPath

Posts   
 
    
kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 10-Apr-2013 15:29:05   

I got this legacy app with llbl v2.6 code that I have now upgraded to v4.0

This code uses refection right and left and I would like to replace it with strong types. Can I write the code below avoiding reflection?


var auditInfo = CreateEntity(EntityType.AuditInfoEntity);

// Can I avoid reflection here?
var something = (IPrefetchPathElement2)auditInfo.GetType().GetProperty("PrefetchPathAuditInfoDetail").GetValue(null, null);

IPrefetchPath2 prefetchPath = new PrefetchPath2(auditInfo.LLBLGenProEntityTypeValue);
prefetchPath.Add(something);

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Apr-2013 19:12:07   

Convert it to what? Is the prefetchPath name hardcoded or did you write this just for demonstration?

kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 11-Apr-2013 08:46:01   

Hi Walaa

I've edited the original post to make it more clear. Basically I would like to avoid reflection and magic strings and use the framework directly

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2013 20:56:31   

Is the prefetchPath name hardcoded or did you write this just for demonstration?

kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 12-Apr-2013 09:11:23   

What do you mean hardcoded or for demo? prefetchPath is


IPrefetchPath2 prefetchPath = new PrefetchPath2(auditInfo.LLBLGenProEntityTypeValue);
prefetchPath.Add(something);

I think that's not even important. What I would like to change is the code before that.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Apr-2013 11:08:31   

Your initial code included "PrefetchPathAuditInfoDetail".

That's what I was asking about. Is this hardcoded as shown in your initial code? i.e. is this for real, or just to demonstrate your issue.

Anyway.

V.4.0 introduces no new solution for this. But there is a technique you can use to avoid reflection.

From your code it appears that you have some entities that should have this property "PrefetchPathAuditInfoDetail", but then since the type of the entity is not known at coding time, you are residing to reflection to access this property.

Well you can define an Interface having this property, then for those entities that share this common property, they should implement that interface. (you can add this in a separate file, for each entity "partial classes" to avoid being overwritten when regenerating the code).

Now in the piece of code at hand, you should cast the auditInfo entity to that Interface, and access the prefetchPath property.

kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 12-Apr-2013 13:18:14   

I didn't realize you were referring to the "PrefetchPathAuditInfoDetail" string, I thought you meant the variable prefetchPath. Yes is real.

That worked perfect, thanks Walaa.