Determining Entities contained in EntityCollection

Posts   
 
    
Posts: 134
Joined: 04-Mar-2005
# Posted on: 02-Jun-2005 20:00:41   

I'm sure this has been asked before but I just can't find a thread on it, so...

Is there any way of determining the EntityType of the Entities in an EntityCollection? I'm passing an EntityCollection to a method and I need to iterate through each Entity refering as I go to a specific property that's on each Entity (but not in IEntity2).

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 02-Jun-2005 20:40:40   

ChicagoKiwi wrote:

I'm sure this has been asked before but I just can't find a thread on it, so... Is there any way of determining the EntityType of the Entities in an EntityCollection? I'm passing an EntityCollection to a method and I need to iterate through each Entity refering as I go to a specific property that's on each Entity (but not in IEntity2).

You can use the "is" keyword.

foreach (IEntity2 entity in entityCollection){
    if (entity is MyEntity){
        MyEntity myEntity = (MyEntity)entity;
        myEntity.MyProperty = "Something";
    }
}

simple_smile

Posts: 134
Joined: 04-Mar-2005
# Posted on: 02-Jun-2005 21:12:37   

Ugh. That means I have to try every EntityType?? disappointed

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 02-Jun-2005 21:31:52   

Depends on how you use the collection. If you store just one type in the collection (and the framework at the moment does that), you can use as Marcus explained and for example check the first entity.

You can also grab a PropertyInfo object for the property on the first entity, then in a try/catch clause call that property. If it's not there, you'll end in the catch clause and swallow the exception as the entity was apparently of a different type. wink

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 02-Jun-2005 21:35:53   

While not a fool-proof solution, you can check the EntityCollection.EntityFactoryName property, but it depends on how you're using it...

Jeff...

Posts: 134
Joined: 04-Mar-2005
# Posted on: 02-Jun-2005 21:36:33   

Otis wrote:

Depends on how you use the collection. If you store just one type in the collection (and the framework at the moment does that), you can use as Marcus explained and for example check the first entity.

You can also grab a PropertyInfo object for the property on the first entity, then in a try/catch clause call that property. If it's not there, you'll end in the catch clause and swallow the exception as the entity was apparently of a different type. wink

And I thought of doing something like that, except that I potentially (although it's unlikely) have an empty EntityCollection. Surely, since the EntityCollection has been created with an entity-specific factory, I should be able to tell the EntityType for the EntityCollection?

Posts: 134
Joined: 04-Mar-2005
# Posted on: 02-Jun-2005 22:32:53   

jeffreygg wrote:

While not a fool-proof solution, you can check the EntityCollection.EntityFactoryName property, but it depends on how you're using it...

Jeff...

I guess that's what I'll have to resort to: stripping "Factory" from the factory name and then turning that into a type...

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 02-Jun-2005 23:09:12   

If what you want is the Type then: EntityCollection.Item(0).GetType

Posts: 134
Joined: 04-Mar-2005
# Posted on: 03-Jun-2005 15:04:33   

Rogelio wrote:

If what you want is the Type then: EntityCollection.Item(0).GetType

That still relies on there actually being an Item in the collection. Most of the time there will be but I'm not sure that I want to, or should have to, rely on that.

Frans: does it make sense to add the EntityType as a property (ItemType?) to the EntityCollection?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 03-Jun-2005 15:23:13   

ChicagoKiwi wrote:

Rogelio wrote:

If what you want is the Type then: EntityCollection.Item(0).GetType

That still relies on there actually being an Item in the collection. Most of the time there will be but I'm not sure that I want to, or should have to, rely on that.

Frans: does it make sense to add the EntityType as a property (ItemType?) to the EntityCollection?

I won't do that, as I'll get problems then with inheritance which is currently in development and which will give problems if you fetch a root type of a hierarchy. simple_smile

The only safe way is to check every entity.

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 03-Jun-2005 22:08:04   

Otis wrote:

ChicagoKiwi wrote:

Rogelio wrote:

If what you want is the Type then: EntityCollection.Item(0).GetType

That still relies on there actually being an Item in the collection. Most of the time there will be but I'm not sure that I want to, or should have to, rely on that.

Frans: does it make sense to add the EntityType as a property (ItemType?) to the EntityCollection?

I won't do that, as I'll get problems then with inheritance which is currently in development and which will give problems if you fetch a root type of a hierarchy. simple_smile

The only safe way is to check every entity.

Methinks the upcoming generics support will be a great solution to this whole issue...

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 03-Jun-2005 23:15:48   

jeffreygg wrote:

Otis wrote:

ChicagoKiwi wrote:

Rogelio wrote:

If what you want is the Type then: EntityCollection.Item(0).GetType

That still relies on there actually being an Item in the collection. Most of the time there will be but I'm not sure that I want to, or should have to, rely on that.

Frans: does it make sense to add the EntityType as a property (ItemType?) to the EntityCollection?

I won't do that, as I'll get problems then with inheritance which is currently in development and which will give problems if you fetch a root type of a hierarchy. simple_smile

The only safe way is to check every entity.

Methinks the upcoming generics support will be a great solution to this whole issue... Jeff...

True, the more I implement it, the more I think it's a heck of a feature simple_smile . Lots of problems along the road to having it of course... But that's another story simple_smile

Frans Bouma | Lead developer LLBLGen Pro