Find out type of entity in an IEntityCollection?

Posts   
 
    
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 27-Nov-2006 15:22:33   

I've got an IEntityCollection object filled with entites of one type. Is it possible to find out the type of the entities in this collection?

I'd like to do something like this: IEntityCollection myColl = blablaEntityCollectionInstance myColl.Add(new ???) (<- in this case, it should be a blablaEntity)

By using reflection I can create a blablaEntity when there is at least one blablaEntity in "myColl". This works fine. But: When the Collection is empty, I've got a problem, because reflection won't work then.

So I'm searching for another way to find out the type of the entity i want to add. Does anybody have a hint for me?

Thanks in advance!

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 27-Nov-2006 15:45:48   

Hello,

You can use that to get a new entity to insert into the entityCollection :


Dim myEntityCollection As sd.LLBLGen.Pro.ORMSupportClasses.IEntityCollection=....
dim myNewEntity as Ientity= myEntityCollection.EntityFactoryToUse.Create()

And after get the type and modify the fields you want.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 27-Nov-2006 16:20:08   

Perfect. This is exactly what I've been looking for. Thank you!

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 13-Feb-2007 12:02:54   

Similar problem: I've got an entity and want to instantiate the appropriate collection. How could I do this?

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 13-Feb-2007 15:11:15   

Hello,

you can create a new EntityCollection of EntityBase2 objects, so you can add the entity you want into the collection ignoring the type. But why do you need an entitycollection if you don't know the entitytype? You can use a generic list of entitybase2 if it's only to store them in a temporary var.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 13-Feb-2007 15:40:50   

But i do know the entity type! As I wrote I have an entity instance, so I also know the type.

I like to do something like this:


blablaEntity ent= new blablaEntity();
CreateCollectionFromEntityType(ent.GetType());

My new question has nothing to do with the problem described at the beginning of this thread, i just thought that it fits, because this is the reverse situation...

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 13-Feb-2007 15:57:41   

Hello,

First of all, what version of llbl(and .net) do you use now? If you use the v2 on framework 2 you have generics entity that can be call using: new entitycollection(of TYPEOFTHEENTITY). In that case you can not use ent.GetType but if you have the type you can manually add it. The other constructor is new entitycollection(entityfactorytouse) and you need to create an entityfactory but here too you need to know what entityfactory to init. You can use a generic collection for the save process if you don't know the type at the moment of you type the code. I understand you have the type at the execution but not at the development.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 13-Feb-2007 16:42:07   

Sorry, I don't get it. Yes, i use LLBLGen v2, with .NET-framework 2.0, C# an SelfServicing Scenario. And yes i know the type in run time and not at design time.

If i understand cou correctly you mean something like this:


EntityCollectionBase<blablaEntity> coll;
coll = new EntityCollectionBase<blablaEntity>();

I know, there's something wrong in the code, but, as you wrote i can't use GetType(), what i must use because i don't know the type in run time. 2nd idea has the same problem. What now?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 13-Feb-2007 16:45:37   

In entity classes there's a protected method 'CreateEntityFactory'. This factory can be used to create a new entity collection. However as jbb said, creating a new generic entity collection, can only be done in code specifying the type.

So you could decide to add a simple method via an include template which does: public EntityCollection<_TypeOfEntity> GetEntitySpecificCollection() { return new EntityCollection<_TypeOfEntity>(); }

where TypeOfEntity is generated into the code, e.g. CustomerEntity

Frans Bouma | Lead developer LLBLGen Pro