Getting entity from database table name

Posts   
 
    
gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 14-Sep-2011 22:07:03   

LLBLGen Pro v3.1 Final (February 7th, 2011)

Hi,

I'm wondering if it's possible to create an entity based on the database table name? I know there is a mapping from database table name to entity is in the internal class PersistenceInfoProviderCore. Is there anything similar I can use to get the entity like that?

Thanks, Gilbert

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Sep-2011 06:54:44   

gilbert wrote:

I'm wondering if it's possible to create an entity based on the database table name? I know there is a mapping from database table name to entity is in the internal class PersistenceInfoProviderCore. Is there anything similar I can use to get the entity like that?

Do you mean "at runtime"? If you didn't add the entity via LLBLGen Designer, it's no possible to create an entity on-the-fly based on some table. So you have to add it in your LLBLGen project and then regenerate code.

David Elizondo | LLBLGen Support Team
gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 15-Sep-2011 15:58:25   

Sorry, should have been clearer.

I already have all my entities generated, it's just that I want a way to instantiate an entity object based on a database table name at runtime. It's because I have a table with a column holding another table's name and I need to create an object during runtime using the name in that column. The objects aren't linked by super-type and sub-type so I cannot use FetchNewEntity and rely on the polymorphic mapping to find it.

So from LLBL documentation example I have a BoardMember entity. It has a reference to Car which is a super-type. Subtypes are family, sport, suv, etc. BoardMember also has a column with the database table name of the type of car they should have (so each board member has a fixed car type assigned car_family_table, car_sport_table, etc). When I create a new BoardMember entry I want to create a new car and add that car's id to the respective subtype table. Therefore I need a way to get the subtype based on the database table name in BoardMember entity.

Hope it doesn't sound too confusing.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Sep-2011 02:27:17   

I see now. Ok, from your previous posts I know you are using Adapter.

This is not a straight forward task, however it could be done. The problem is that the DataAdapterClass and PersistenceInfoProvider don't know anything about your specific entity classes. The most you can do over there is to obtain the table name for a passes IEntity2. Example:

public partial class DataAccessAdapter
{   
    public string GetTableName(string entityName)
    {
        return this.GetFieldPersistenceInfos(entityName)[0].SourceObjectName;
    }
}

However, with that code you can create a routine in your business code that finds the entity based on your table name:

public IEntity2 GetEntityFromTableName(string tableNameToFind)
{
    IEntity2 toReturn;
    using (var adapter = new DataAccessAdapter())
    {
        var entityAsString = (from e in Enum.GetNames(typeof(EntityType))
                                let tableName = adapter.GetTableName(e)
                                where tableName == tableNameToFind
                                select e).FirstOrDefault();

        toReturn = FactoryClasses.GeneralEntityFactory.Create(
            (EntityType)Enum.Parse(typeof(EntityType), entityAsString));
    }

    return toReturn;
}

.. and then use it:

[TestMethod]
public void GetEntityFromTableName()
{
    IEntity2 entity = GetEntityFromTableName("Customers");

    Assert.IsTrue(entity is CustomerEntity);
}

Hope helpful.

David Elizondo | LLBLGen Support Team