Getting General EntityTypes

Posts   
 
    
Rob
User
Posts: 54
Joined: 17-Sep-2004
# Posted on: 20-May-2008 06:35:26   

Hi!

I am starting construct a procedure along the lines below.


private SD.LLBLGen.Pro.ORMSupportClasses.IEntity getEntity(string type, object id, object id2)
{
    SD.LLBLGen.Pro.ORMSupportClasses.IEntity entity;
    switch(type)
    {
        case "BizEntityEntity":
            BizEntityEntity bizEntity = new BizEntityEntity();
            if(id != null)
                bizEntity.FetchUsingPK((int)id);
            return bizEntity;
        case "Person_OfficeEntity":
            Person_OfficeEntity person_Office = new Person_OfficeEntity;
            if(id != null && id2 != null)
                person_Office.FetchUsingPK((int)id, (int)id2);
            return person_Office;
        default:
            throw new Exception("Unsupported Type: The procedure getEntity has been passed a type it does not support.");
    }
}

The purpose is to be able to pass the EntityType as a string and get back the correct entity.

I realise that everytime I create a new EntityType in my database I will need to manually add it as a new case to this procedure. I thought that maybe there is a more clever way to use it in the SelfServicing framework?

I currently have 202 tables in the database it there are new ones coming in every once in a while.

Grateful for any tips!

!Rob

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-May-2008 07:09:52   
David Elizondo | LLBLGen Support Team
Rob
User
Posts: 54
Joined: 17-Sep-2004
# Posted on: 20-May-2008 07:43:02   

Thanks daelmo

It is obviously on my subject. That example has the problem of having to exclude certain EntityTypes and also it involves making changes here and there in the generated code. I try to steer clear of doing that. I like all my code within the "Custom Entity code" region. Mostly because I feel safe there and also because I regenerate very often since I add tables and fields all the time.

Is there no way I can call on existing code to get back what I need to do my thing?

I tried to decipher Otis reply in the thread you pointed me to but was utterly lost.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 20-May-2008 11:07:10   

If you have the entity name and you want to get the entityType, use the following:

string entityName = "ModuleEntity";         
EntityType myEntityType = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);

If you have the entityType and you need the entityFactory, use the following:

GeneralEntityFactory.Create(myEntityType);

And if you have the entityFactory, then you can go everywhere simple_smile

Rob
User
Posts: 54
Joined: 17-Sep-2004
# Posted on: 20-May-2008 12:14:29   

Thanks Walaa!

I am afraid I lack the knowledge required to finish this using your hint.

As you can see from the code below I pass into the proc a string representation of the EntityType. To get the terminology right I mean to say that for me the EntityType is tablename followed by the word Entity. So in my example I have a table by the name of BizEntity and this follows that my EntityType becomes BizEntityEntity.


private SD.LLBLGen.Pro.ORMSupportClasses.IEntity getEntity(string type, object id, object id2)
{
    SD.LLBLGen.Pro.ORMSupportClasses.IEntity entity;
    switch(type)
    {
        case "BizEntityEntity":
            BizEntityEntity bizEntity = new BizEntityEntity();
            if(id != null)
                bizEntity.FetchUsingPK((int)id);
            return bizEntity;
        case "Person_OfficeEntity":
            Person_OfficeEntity person_Office = new Person_OfficeEntity;
            if(id != null && id2 != null)
                person_Office.FetchUsingPK((int)id, (int)id2);
            return person_Office;
        default:
            throw new Exception("Unsupported Type: The procedure getEntity has been passed a type it does not support.");
    }
}

So if I use your code example I could perhaps do this:


private SD.LLBLGen.Pro.ORMSupportClasses.IEntity getEntity(string type, object id, object id2)
{
    EntityType myEntityType = (EntityType)Enum.Parse(typeof(EntityType), type, false);
}

But inthe next step, how do I use:

GeneralEntityFactory.Create(myEntityType);

to instantiate an object of my entity type?

GeneralEntityFactory.Create appears to return an IEntity. I need the type described in the EntityType enum.

A simple example would probably explain the whole thing to me.

best regards,

!Rob

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 20-May-2008 21:09:55   

Example using Northwind:


private SD.LLBLGen.Pro.ORMSupportClasses.IEntity2 getEntity(string type, object id)
        {

            //Dictionary<int,object> uniquekey
            EntityType myEntityType = (EntityType)Enum.Parse(typeof(EntityType), type, false);
            IEntity2 entityInstance = GeneralEntityFactory.Create(myEntityType);
            IEntity2 loadedEntity = Activator.CreateInstance(entityInstance.GetType(), new object[] { id }) as IEntity2;

            adapter.FetchEntity(loadedEntity);
            return loadedEntity;

        }

How to call the above:

 OrdersEntity o = (OrdersEntity) Convert.ChangeType(getEntity("OrdersEntity", 10248),typeof(OrdersEntity));