Getting entity fields from datasource EntityTypeName

Posts   
 
    
benjam47
User
Posts: 13
Joined: 09-Apr-2008
# Posted on: 19-Jun-2008 02:19:36   

Hi,

I am creating a grid that will be generated dynamically to perform general table lookups in our system. We simply pass the name of the EntityFactory that we want to view in the LLBL's DataSource EntityFactoryTypeName, and that all work great.

However, I need to be able to search through the fields in the table to see if a certain field exists, and if it does I need to add a filter to the datasource.

Here is what I have so far:

            EntityFactoryBase2 ef = new FrameworkUserEntityFactory();
            IEntityFields2 eflds = ef.CreateFields();
            foreach (EntityField2 efld in eflds)
            {
                if (efld.Name == "ClientID")
                {
                     IPredicate filter;
                     filters.PredicateExpression.Add(FrameworkUserFields.ClientId == ClientID);
                    // ADD ClientID filter to datasource
                }
            }

My first problem is the first line, which is presently hardcoded to create a specific EntityFactory (FramworkUser). I need to change this line so that it creates an entity factory as specified in the datasource EntityFactoryTypeName property. I assume I need to use reflection to do this, but I'm not familiar with how. Can anyone point me in the right direction?

My second problem is the PredicateExpression.Add line, which is again presently hardcoded to a specific entity's Fields class. Again any feedback on how I can access the Fields class for an entity specified by the datasource EntityFactoryTypeName property would be appreciated.

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 19-Jun-2008 15:53:24   

My first problem is the first line, which is presently hardcoded to create a specific EntityFactory (FramworkUser). I need to change this line so that it creates an entity factory as specified in the datasource EntityFactoryTypeName property. I assume I need to use reflection to do this, but I'm not familiar with how. Can anyone point me in the right direction?

Use Activator.CreateInstance() to create an instance of the required entityFactory.

(IEntityFactory2)Activator.CreateInstance( typeToCreate);

Btw, I also think you can directly use the EntityFactoryToUse property of the datasource.

myDataSource.EntityFactoryToUse

Which is created automatically using the same Activator.CreateInstance() code from the EntityFactoryTypeName value.

My second problem is the PredicateExpression.Add line, which is again presently hardcoded to a specific entity's Fields class. Again any feedback on how I can access the Fields class for an entity specified by the datasource EntityFactoryTypeName property would be appreciated

Having an entity instance, you can do the following to get an entityField by its name:

EntityField2 field = myEntity.Fields["ClientId"];
benjam47
User
Posts: 13
Joined: 09-Apr-2008
# Posted on: 23-Jun-2008 22:12:15   

The data source control does not have a EntityFactoryToUse property, but I was able to make it using Activactor.CreateInstance as you suggested.

Everything is working now. Thanks again.