How to dynamically instantiate the EntityField2 type in .NET CORE 3.1 and LLBLGen Pro 5.8

Posts   
 
    
AlbiLot
User
Posts: 5
Joined: 03-Sep-2020
# Posted on: 09-Aug-2021 00:22:18   

Hi team,

I would like to dynamically instantiate the EntityField2 type programmatically in .NET CORE. I was able to do this in .NET 4.0 using this line of code: EntityField2 fld = (EntityField2)EntityFieldFactory.Create("UserEntity", "FirstName");

This code resulted in an instance of EntityField2 class for the UserEntity.FirstName field, which I could use in predicate expressions.

I didn't find the EntityFieldFactory in generated .NET CORE Standard 2.1 code. How can I achieve the same scenario, so that I can get EntityField2 instance using a string parameter with the name of the entity and another string parameter with the name of the field?

Best regards, Albin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 09-Aug-2021 09:18:45   

You can do that using var myField = UserFields.FirstName;

Nowadays you don't need to use the arcane string based approach (which is error prone). Also look into the extension methods in the QuerySpec namespace which help you create predicates way easier. https://www.llblgen.com/Documentation/5.8/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Filtering%20and%20Sorting/gencode_filteringpredicateclasses.htm

Frans Bouma | Lead developer LLBLGen Pro
AlbiLot
User
Posts: 5
Joined: 03-Sep-2020
# Posted on: 11-Aug-2021 00:46:21   

Thank you for your explanation. I totally agree. I know the QuerySpec namespace.

My problem is, I don't know until the runtime, what entity and field I will have to use in predicate expression. The user can create "dynamic" query criteria by selecting the entity and the field. And I have to create its instance in order to use it in predicate expression. For that reason I am using Low level API in C# (because it seems to me that I am a bit more flexible) and my own generic method which accepts string parameters for the entity and field.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 11-Aug-2021 09:22:18   

Then you could use the ModelInfoProviderSingleton to get the field info using the strings you have. Alternatively you can use the ModelInfoProviderSingleton to create a field for you based on the EntityField enum index belonging to your field.

so:

var myField = new EntityField2(ModelInfoProviderSingleton.GetInstance().GetFieldInfo(entityName, fieldName);

// alternative where you use your strings to get the enum value. 
var myField = ModelInfoProviderSingleton.GetInstance().CreateField2(CustomerFieldIndex.CompanyName);

Another alternative is to use an entity instance or the full field array. An entity instance's Fields property can be indexed using the name of the field to obtain an instance. Yet another alternative is to use the ModelInfoProviderSingleton to create all the fields for an entity using its GetEntityFields(entityname) method and index in that object using the field name. This is a little wasteful tho.

Frans Bouma | Lead developer LLBLGen Pro
AlbiLot
User
Posts: 5
Joined: 03-Sep-2020
# Posted on: 11-Aug-2021 22:24:20   

Thank you very much for your help! Problem solved :-)