Casting TargetPerEntityHierarchy types

Posts   
 
    
jsaylor
User
Posts: 37
Joined: 20-Sep-2004
# Posted on: 25-Nov-2008 22:26:00   

I've recently discovered how to put TargetPerEntityHierarchy to good use and am making the most of it. sunglasses

One thing that I've run into though and am wondering how best to handle it are the casting issues in relation to the various entities. I know this is part OO 101, but I'm interested in the LL specifics of it!

Say I've got this heirarchy:

Base
    |-Person
        |-Manager

So each derives from (and is castable to) a BaseEntity obj. But how best can I do the following:

  • 1. Determine what sub-type a particular obj is (Base, Person, or Manager)?
  • 2. How can I downcast to a specific type once I've determined it? I was hoping Person or Manager might have a "New(e as IEntity)" so that I could just create a new obj of the specific type and pass in the base, but no go (and I'm thinking if it's not already there, well there's a good reason for it) confused
  • 3. Assume that Manager has relation that the sub-types do not. How can I best dynamically determine that by inspecting the obj?

(also, I'm in .NET 2.0 - so no 3.5 wizardry allowed)

As always, LL rocks - the more I learn, the better it is!

Appreciation in advance!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Nov-2008 05:37:27   

jsaylor wrote:

  1. Determine what sub-type a particular obj is (Base, Person, or Manager)?
someEntity.GetType() 

or

if (someEntity is ManagerEntity)

jsaylor wrote:

  1. How can I downcast to a specific type once I've determined it? I was hoping Person or Manager might have a "New(e as IEntity)" so that I could just create a new obj of the specific type and pass in the base, but no go (and I'm thinking if it's not already there, well there's a good reason for it) confused

The best natural thing is casting:

ManagerEntity m =  (ManagerEntity) someEntity;

jsaylor wrote:

  1. Assume that Manager has relation that the sub-types do not. How can I best dynamically determine that by inspecting the obj?

Is that possible (a subtype that doesn't have the parent relations?). Please elaborate more.

David Elizondo | LLBLGen Support Team
jsaylor
User
Posts: 37
Joined: 20-Sep-2004
# Posted on: 26-Nov-2008 16:54:18   

daelmo wrote:

jsaylor wrote:

  1. Determine what sub-type a particular obj is (Base, Person, or Manager)?
someEntity.GetType() 

or

if (someEntity is ManagerEntity)

Ok.

daelmo wrote:

jsaylor wrote:

  1. How can I downcast to a specific type once I've determined it? I was hoping Person or Manager might have a "New(e as IEntity)" so that I could just create a new obj of the specific type and pass in the base, but no go (and I'm thinking if it's not already there, well there's a good reason for it) confused

The best natural thing is casting:

ManagerEntity m =  (ManagerEntity) someEntity;

Hmmm, I'm thinking C# differs from VB here (and I think we're into covariance territory, though I'm not an expert in this area). Let's take Person instead of Manager, since they're in the middle of the hierarchy. In both VB/C#, this works because we casting up the hierarchy (specific->general):

C#: (BaseEntity) b = (BaseEntity) somePersonEntity;
VB: Dim b as BaseEntity = CType( somePersonEntity, BaseEntity)

But, casting down the heirarchy (general->specific) only works in C#:

C#: (PersonEntity) p = (PersonEntity) someBaseEntity;

Try and do this in VB and you'll get an error (with Option Strict On, and presuming the original obj was created as BaseEntity):

VB: Dim p as PersonEntity = CType( someBaseEntity, PersonEntity)

Unfortunately, I'm still not really well versed on generics but I have casually read some stuff that leads me to believe that with them you can achieve primarily the same thing. I was hoping somebody might have some insight how to best do this on the VB side with LL.

daelmo wrote:

jsaylor wrote:

  1. Assume that Manager has relation that the sub-types do not. How can I best dynamically determine that by inspecting the obj?

Is that possible (a subtype that doesn't have the parent relations?). Please elaborate more.

Let's say the base table has a nullable FK to another table called ManagementStuff. You can then select the Manager entity in the hierarchy and create a relation to ManagementStuff that supertypes (Base and Person) don't have. How is it best to "inspect" (hopefully not using reflection) the Entity in question to determine what relations it might have?

Ultimately, what I'm trying to avoid is littering case (switch in C#) statements in the code that handle Base, Person, and Manager differently. What I'd rather do is query the objects to determine what they're capabilities are and then drive decisions on it: mostly UI - for example, display the ManagementStuff editor only if we have a Manager selected.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 26-Nov-2008 17:10:11   

The answer to all your question is that most probably you won't need any of these. !!

That's because in this case LLBLGenPro uses Polymorphic Fetches. Please search for this term in the user manual.

When fetching entities of type base, you will get a collection of entities, each instatiated from its specific type, so you won't need to down cast anything.

jsaylor
User
Posts: 37
Joined: 20-Sep-2004
# Posted on: 26-Nov-2008 18:40:54   

Walaa wrote:

The answer to all your question is that most probably you won't need any of these. !!

That's because in this case LLBLGenPro uses Polymorphic Fetches. Please search for this term in the user manual.

When fetching entities of type base, you will get a collection of entities, each instatiated from its specific type, so you won't need to down cast anything.

sunglasses smile stuck_out_tongue_winking_eye

I only briefly looked at the docs, but on first blush it looks like exactly what I was hoping for!

This is what I mean about LL - the more I learn about it, the happier I am.

With too many other products, when I hit a brick-wall during an arc of effort, it's the end of the road (sorry, the product just doesn't do that). I'm thrilled to say that, most of the time (indeed, so far I think 100% of the time!) LL is always a step or two ahead.

Thanks again, Cheers - Jeff

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Nov-2008 06:31:00   

Thanks for the feedback Jeff simple_smile

David Elizondo | LLBLGen Support Team