IEntity Relations

Posts   
 
    
hawk
User
Posts: 2
Joined: 11-Apr-2007
# Posted on: 11-Apr-2007 13:20:22   

I’ve created a method which accepts an IEntity object, table name (string) and column name (string). The method loops through all fields in the IEntity object and if it finds a field with the same name as the column name passed into the method it returns its value, as seen below.

for (int f = 0; f < _objEntity.Fields.Count; f++) { if (_objEntity.Fields[f].Name == FieldName) { if (_objEntity.Fields[f].CurrentValue.ToString() == FieldValue) returnBool = true; } }

The problem we’ve got is that sometimes the table name passed into this method isn’t the name of the IEntity table. In these cases I’ve created a method that loops through all tables related to the original IEntity object to see whether its name matches the table name passed into the method, as seen below.

List<IEntity> relations = _objEntity.GetDependentRelatedEntities(); foreach(IEntity iEnt in relations) { if (iEnt.LLBLGenProEntityName == EntityName) { _objEntity = iEnt; break; } }

This pretty much works perfectly, except for the fact that “GetDependentRelatedEntities()” only actually returns anything IF PreFetchPaths were used when fetching the data for the IEntity object. It seems to me that it would be rather silly to have to force every single preFetchPath on the IEntity so that the “GetDependentRelatedEntities()” method works properly.

Is there any way to loop through all related tables on an IEntity object, compare the name of the related table with a local variable and then IF a match is found actually populate that related entity with data (based on its relationship with the original IEntity)?? As a test I placed the following line of code just before the “GetDependentRelatedEntities()” line above. Doing this then meant that when executing “GetDependentRelatedEntities()” it finds and returns the Client table relation... everything then works perfectly. This is of course a complete hack and assums that if the related table will ALWAYS be “Client”.

((UserEntity)objEntity).Client.Refetch();

This is proving to be a mamoth of a problem for us, any help would be much appreciated.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Apr-2007 17:16:08   

From what I understand, I think you need to loop through (the static property of the entityClass) the Relations collections, eg: relations = CustomerEntity.Relations

hawk
User
Posts: 2
Joined: 11-Apr-2007
# Posted on: 12-Apr-2007 17:25:43   

Thanks.

I think we are almost there BUT your solution assumes that we know the type of the entity (example: UserEntity object). However this is not the case.

our method accepts IEntity object as a parameter. Is there any way to either cast it dynamically to give us the correct type of object (so we can iterate through the relations of that object) or is any way to access the relations for that IEntity object directly?

Thanks again!

update:

we have just tried looping through each relation belonging to "UserEntity" and it gives the following error:

foreach statement cannot operate on variables of type 'VIPex.BackOffice.RelationClasses.UserRelations' because 'VIPex.BackOffice.RelationClasses.UserRelations' does not contain a public definition for 'GetEnumerator'

UserRelations relations = UserEntity.Relations; foreach (IEntityRelation rel in relations) { if (rel.AliasPKSide == EntityToFetchFrom) { string f = "boo"; } }

once again, please bear in mind that we still dont know the type of object we are dealing with - for temp/test purposes we are using this 1 strongly known type.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39922
Joined: 17-Aug-2003
# Posted on: 13-Apr-2007 18:41:49   

It's often best to also explain a little bit why you want to do this simple_smile .

the info you're looking for isn't available to you at runtime. So you need to provide it yourself. This is pretty straight forward: create a new .lpt template which simply generates a class with static methods which have a hashtable setup with the meta-information you need and generate the class with a new task in your preset. Then at runtime you consult your meta-class you just generated and obtain the info you need. .lpt templates have access to the complete object model of LLBLGen Pro so you can generate all meta-data into the code.

For reading the value, you can simply do: IEntityField f = entity.Fields[name]; if(f!=null) { // present return f.CurrentValue; }

Frans Bouma | Lead developer LLBLGen Pro