Creating a new collection given an entity type

Posts   
 
    
Posts: 17
Joined: 23-Feb-2007
# Posted on: 08-May-2008 16:57:50   

I have a generic class with the generic parameter constrained to entity types. I need a way to get a new collection of the given type. That is, I need to implement this method...

public class MyClass<TEntity> where TEntity : CommonEntityBase
{
    // If TEntity is ProductEntity, return a new ProductCollection
    // If TEntity is CustomerEntity, return a new CustomerCollection
    // etc
    protected IEntityCollection GetTEntityCollection()
    {
        ...
    }
}

Here's the best implementation I have so far. Is there a way to do it without using reflection and creating an entity instance like I have done?

public class MyClass<TEntity> where TEntity : CommonEntityBase
{
    protected IEntityCollection GetTEntityCollection()
    {
        IEntity entity = Activator.CreateInstance(genericEntityType) as TEntity;
        EntityType entityType = (EntityType)Enum.ToObject(typeof(EntityType), entity.LLBLGenProEntityTypeValue);
        return GeneralEntityCollectionFactory.Create(entityType);
    }
}

I'm using LLBLGen Pro 2.5 with the SelfServicing, two class scenario.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 09-May-2008 09:26:03   

Please use the GeneralEntityFactory.Create(EntityType entityTypeToCreate) method.

Posts: 17
Joined: 23-Feb-2007
# Posted on: 13-May-2008 21:06:06   

My problem is that I don't have the EntityType. I have the type TEntity (which I know is is a subtype of CommonEntityBase) and I'd like to get EntityType.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 14-May-2008 09:51:13   
string entityName = "ModuleEntity";         

EntityType typeOfEntity = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);
Posts: 17
Joined: 23-Feb-2007
# Posted on: 14-May-2008 16:03:49   

Good idea. Thanks.