Dynamic fetch Entity by EntityType and PK

Posts   
 
    
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 29-May-2007 16:49:39   

Hi All,

I found this brilliant solution by Otis:

"One other way would have been by using a projection, fetch a dyn. list using the fields of the entity instance and a filter on the PK field, and the factory to produce the entity instance."

See: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=8445 (last message)

I need this, but unfortunatly I have no clue what is meant by it simple_smile

Anyone any pointers?

TIA,

Gab

gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 29-May-2007 17:09:09   

Hi,

One way is:

            // Set the name
            string entityName = "ModuleEntity";         

            EntityType typeOfEntity = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);
            IEntity entityInstance = GeneralEntityFactory.Create(typeOfEntity);
            IEntity loadedEntity = Activator.CreateInstance(entityInstance.GetType(), new object[] { 2 }) as IEntity;

            // Fetch info from the retrieved entity
            this.Title = loadedEntity.Fields["NameShort"].CurrentValue.ToString();

Where "2" is the primary key value and "ModuleEntity" the entity name

But I'm still looking for the other solution and maybe better performing solutions.

Cheers, Gab

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-May-2007 10:53:36   

Since the goal is to have an Entity instance, I'd have done it using the Activator.CreateInstance() method (same as you did).