How to create a new entity from GetType() info

Posts   
 
    
tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 26-Dec-2006 05:05:30   

Environment: LLBLGenPro Demo (latest version) .Net 2.0 SelfServicing Model

I need to generate a new LLBLGenPro Entity. The only information I have is the GetType() info from an existing entity of the same type. Is there a way to do this?

for example:

newEntity = EntityFactory(existingEntity.GetType())

The newEntity needs to get its starting values from OnInitialization().

This functionality is need to write a custom BindingNavigator component.

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Dec-2006 08:14:33   

Use the CreateInstance method of the .NET Activator class.

PilotBob
User
Posts: 105
Joined: 29-Jul-2005
# Posted on: 26-Dec-2006 23:53:12   

I haven't tried this but have you looked at the EntityFactoryConverter class? It looks like you can convert the factory type to a concrete factory.

Probably Fran would know how to do this.

If that doesn't work, you could edit your Entity template and add a GetFactory method that returns the factory for the entity. To tell you the truth I am surprised this doesn't already exist.

BOb

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Dec-2006 08:16:06   

In the generated code, under the Factory Classes, there is a class GeneralEntityFactory where you can use its create method to create your entity based on the type.


public static IEntity2 Create(EntityType entityTypeToCreate)
{
    IEntityFactory2 factoryToUse = null;
    switch(entityTypeToCreate)
    {
        case POT.EntityType.ActivityEntity:
            factoryToUse = new ActivityEntityFactory();
            break;
        case POT.EntityType.ClientEntity:
            factoryToUse = new ClientEntityFactory();
            break;
    }
    return factoryToUse.Create();
}       

Although I find it useless, but you can modify the templates to generate another similar method that only returns the factoryToUse without creating the entity.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 27-Dec-2006 09:03:38   

If a Type instance is known (GetType() result), the Activator class of .NET is the best and easiest way to create a new instance.

Frans Bouma | Lead developer LLBLGen Pro