Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> Getting General EntityTypes
 

Pages: 1
LLBLGen Pro Runtime Framework
Getting General EntityTypes
Page:1/1 

  Print all messages in this thread  
Poster Message
Rob
User



Location:
Gold Coast, Queensland, Australia
Joined on:
17-Sep-2004 13:51:52
Posted:
50 posts
# Posted on: 20-May-2008 06:35:26.  
Hi!

I am starting construct a procedure along the lines below.

Code:

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
  Top
daelmo
Support Team



Location:
Guatemala City
Joined on:
28-Nov-2005 23:35:24
Posted:
8041 posts
# Posted on: 20-May-2008 07:09:52.  
Maybe this would be helpful: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=8445

David Elizondo
LLBLGen'ing (articles and code snippets) | linkedin | twitter
 
Top
Rob
User



Location:
Gold Coast, Queensland, Australia
Joined on:
17-Sep-2004 13:51:52
Posted:
50 posts
# 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.
  Top
Walaa
Support Team



Location:

Joined on:
21-Aug-2005 16:03:48
Posted:
14449 posts
# Posted on: 20-May-2008 11:07:10.  
If you have the entity name and you want to get the entityType, use the following:
Code:
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:
Code:
GeneralEntityFactory.Create(myEntityType);


And if you have the entityFactory, then you can go everywhere Regular Smiley


  Top
Rob
User



Location:
Gold Coast, Queensland, Australia
Joined on:
17-Sep-2004 13:51:52
Posted:
50 posts
# 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.

Code:

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:
Code:

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:
Code:
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
  Top
goose
User



Location:
Central America
Joined on:
06-Aug-2007 18:21:05
Posted:
385 posts
# Posted on: 20-May-2008 21:09:55.  
Example using Northwind:

Code:

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:

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


gansodesoya  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.