using an entity collection's base type as property type

Posts   
 
    
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 20-Sep-2006 16:15:34   

Hi, I want to use an entity collection as a property. Because i want to use different kinds of entity collections, the property's type should be of the parent type of a Collection Base. But the base class of an entity collection is an generic list. So: how do i pass a generic list whose item's types i don't know yet?

For example I have a class1 with something like this:


private EntityCollectionBase<EntityBase> m_Collection;

public EntityCollectionBase<EntityBase> Collection {
  get { return m_Collection; }
  set { m_Collection = value; }
}

and another wants to access the property like this:


classInst1.Collection = new blablaEntityCollection(); (could be any other entity collection)

I know this is not a LLBLGen prob, it has to do with the .NET generics. Perhaps somebody could just paste a bit of correct code? This would be great!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Sep-2006 16:25:31   

I think you should instantiate it as follows:

classInst1.Collection = new blablaEntityCollection<blablaEntity>(new blablaEntityFactory());
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 20-Sep-2006 16:34:40   

Ok, and what do I do if i have already an entity collection?

"classInst1.Collection = blablaEntityCollectionInstance" doesn't work.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 20-Sep-2006 16:38:42   

Maybe try the following:

blablaEntityCollection<blablaEntity> alreadyBlaBla = new blablaEntityCollection<blablaEntity>(new blablaEntityFactory());

classInst1.Collection = alreadyBlaBla;
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 20-Sep-2006 16:52:15   

Hm. I have to confess that I don't understand this factory stuff - where can i find information what this is?

I create my entity collections like that:


blablaEntityCollection myColl = new blablaEntityCollection();
myColl.GetMulti(null);

Now I want to pass the collection to a object with a property which accepts myColl and any other collection created by LLBGen. I don't trust my property syntax. Is this the correct syntax for doing this?

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 21-Sep-2006 09:47:34   

Walaa wrote:

Maybe try the following:

blablaEntityCollection<blablaEntity> alreadyBlaBla = new blablaEntityCollection<blablaEntity>(new blablaEntityFactory());

classInst1.Collection = alreadyBlaBla;

Ok, i think i understand now what a factory is and see why i could use it. But of which type is blablaEntityCollection? As i wrote before, this could be a different collection type each time the code is executed. I want a property which accepts any entity collection LLBLGen creates!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Sep-2006 09:55:54   

And that's why you have used the base type in your declaration of sucj property.

public EntityCollectionBase<EntityBase> Collection 
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 21-Sep-2006 10:00:39   

Best to use IEntityCollection instead of EntityCollectionBase<type> I think, because of the lack of support for co-variance in C#/VB.NET

The interfaces are meant for this purpose: different types of collections, all usable with the same property.

public IEntityCollection Collection.

Frans Bouma | Lead developer LLBLGen Pro
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 21-Sep-2006 10:57:07   

Otis wrote:

Best to use IEntityCollection instead of EntityCollectionBase<type> I think, because of the lack of support for co-variance in C#/VB.NET

The interfaces are meant for this purpose: different types of collections, all usable with the same property.

public IEntityCollection Collection.

Yes, this is the solution. The IEntityCollection is exactly what I was looking for. Now this is possible:


(class1)
private IEntityCollection m_Collection;

public IEntityCollection Collection {
    get { return m_Collection; }
    set { m_Collection = value; }
}

and


class1Inst1.Collection = new blablaACollection();
class1Inst2.Collection = new blablaBCollection();

Wasn't that difficult wink Thanks everyone for your help!

By the way, there's an EntityType-Enumeration. Is there an EntityCollectionType-Enumeration, too? Or is it somehow possible to get the type of the associated collection of a certain entity?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 21-Sep-2006 12:15:53   

Sokon1 wrote:

By the way, there's an EntityType-Enumeration. Is there an EntityCollectionType-Enumeration, too? Or is it somehow possible to get the type of the associated collection of a certain entity?

The collections are always named as: _entityName_Collection, so you can use the EntityType enumeration value, use it as a string and append Collection and use Activator.CreateInstance to create an instance simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 21-Sep-2006 13:02:22   

Yeah - this is a possibility I've already thought about. I just wondered if there was a more "formal" way wink Thanks for your answer!