Determining subtype from supertype entity

Posts   
 
    
wojt
User
Posts: 20
Joined: 27-Oct-2005
# Posted on: 27-Oct-2005 05:35:02   

Hi,

Am evaluating LLBLGen at the moment, and am very impressed.

Is there any way to determine the subtype from a supertype entity without using a discriminator column?

For example, the supertype is Client. There are two types of clients, both subtypes of the Client entity - Individual and Organisation.

If I use the following code...

ClientEntity _client = new ClientEntity(clientId);

...I would like to be able to determine whether the client is an individual or an organisation. Any easy way of doing this?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 27-Oct-2005 11:10:26   

Just use .NET constructs for that: if(_client is Individual) { // it's an individual } else ...

Frans Bouma | Lead developer LLBLGen Pro
wojt
User
Posts: 20
Joined: 27-Oct-2005
# Posted on: 28-Oct-2005 14:15:33   

That is what I tried initially, but it didn't seem to work.

I will play with it a bit more when I get back to my coding machine.

Thanks for your help in the other thread too!

wojt
User
Posts: 20
Joined: 27-Oct-2005
# Posted on: 05-Nov-2005 03:42:56   

I have just tested this (_client is IndividualEntity) and it definitely doesn't work.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 05-Nov-2005 12:12:43   

wojt wrote:

I have just tested this (_client is IndividualEntity) and it definitely doesn't work.

Could you place a breakpoint on that line and in the debugger check what type '_client' is ?

Frans Bouma | Lead developer LLBLGen Pro
wojt
User
Posts: 20
Joined: 27-Oct-2005
# Posted on: 05-Nov-2005 21:39:34   

Otis wrote:

Could you place a breakpoint on that line and in the debugger check what type '_client' is ?

Its a ClientEntity.

  1. It does cast correctly to the subtype, but I can't work out what the subtype is without a discriminator column.
  2. SetDataBindings() works on controls (i.e. they can access the subtype fields).
  3. Was happening in VS.NET 2003 as well as in VS.NET 2005 beta2. Don't have RTM yet.
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 06-Nov-2005 11:30:38   

I've quoted your original posting. It's completely my fault, I should have read your original posting better, then it would have been solved much earlier. Sorry for that.

wojt wrote:

Hi, Is there any way to determine the subtype from a supertype entity without using a discriminator column?

For example, the supertype is Client. There are two types of clients, both subtypes of the Client entity - Individual and Organisation.

If I use the following code...

ClientEntity _client = new ClientEntity(clientId);

...I would like to be able to determine whether the client is an individual or an organisation. Any easy way of doing this?

Now, your code snippet has an explicit new ClientEntity() statement. This means that .NET will create a ClientEntity object. No matter what the type is of the entity identified by clientId, the object will be of type ClientEntity, due to the new operator.

To get a polymorphic fetch, and to get an instance of the type represented by the value clientId, you should do:


ClientEntity _client = ClientEntity.FetchPolymorphic(null, clientId, null);

LLBLGen Pro will then create an instance of the entity type represented by clientId and will return that. _client can then be of a different type. This works because ClientEntity is a supertype of the subtypes which can be produced. 'is' then works.

Frans Bouma | Lead developer LLBLGen Pro
wojt
User
Posts: 20
Joined: 27-Oct-2005
# Posted on: 07-Nov-2005 01:05:06   

That's great, thanks very much for your help! I am reading the manual, but it takes time! ;-)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39625
Joined: 17-Aug-2003
# Posted on: 07-Nov-2005 11:52:33   

wojt wrote:

That's great, thanks very much for your help! I am reading the manual, but it takes time! ;-)

simple_smile And thanks for your kind words simple_smile

Frans Bouma | Lead developer LLBLGen Pro