How do I get an entity's related entity without using reflection or the related entity's property? (Example: Patient.Address)

Posts   
 
    
Jeremy Driver avatar
Posts: 41
Joined: 08-May-2004
# Posted on: 25-Apr-2006 20:40:26   

Hi,

Let's say that I have two entities: Patient and Address. Patient has a foreign key link to Address, so I can get the patient's address in code by: 'AddressEntity address = patient.Address;'.

What I need to to is to get the Address entity from the Patient entity without using the Patient entity's Address property. If I want to access a field value without using its property, I can use the Fields collection as long as I know the field's index. I need a similar method to access an entity's related entities.

Is there any way to do this without using reflection?

Thanks, Jeremy

mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 25-Apr-2006 22:39:01   

You can use Patient.GetDependentRelatedEntities to get an EntityCollection of the linked objects and loop through it to find the one you're looking for. Unfortunately, you won't be able to find the Address object on the Patient entity if the Patient entity has more than one AddressEntities hanging off of it (unless you know the PK or something). But, if Patient only has one AddressEntity, this should work.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 26-Apr-2006 10:00:30   

If you need to access entities which are related to a patient, you (IMHO) need to know what type they are, isn't it logical to build that into your code? Why search for a generic way while your code is tied to a specific situation, or am I missing something ? (probably wink )

Frans Bouma | Lead developer LLBLGen Pro
Jeremy Driver avatar
Posts: 41
Joined: 08-May-2004
# Posted on: 27-Apr-2006 23:40:43   

Frans,

I'm rolling my own data binding due to special requirements of my app. I've created various binding objects based on field types. In order to make my objects as generic as possible, I give these binding objects the field index of the field they'll bind to when I create them. When its time to save the entity, I pass in the entity which will receive the control's value.

Example:

PhoneNumberBinder Validates, parses, formats, phone number values Has FieldIndex property Has PhoneNumberControl property Has SetEntityFieldValue(IEntity2 entityToSave) method Has SetControlValue(IEntity2 entityToSave) method

When its time to save the entity, I call 'SetEntityFieldValue(entityToSave)'

Now I can use this object with any entity adding/editing screen that adds/edits an entity that has a phone number.

This works great until I have an entity adding/editing screen which adds/edits an entity with child entities. I need to be able to get child entities from their parent entities in a generic way (similar to the way fields can be generically accessed using the IEntity2.Fields collection) so that I can pass the child entities to their custom binding objects. I hope this makes sense. disappointed

Anyway, I'm guessing I'll have to use reflection since I don't see any methods/properties in IEntity2 that will allow me to have generic access to related entities or entity collections generically like I can with IEntity2.Fields. Any thoughts?

And thanks in advance. simple_smile

PS

Mike: Thanks for the tip. It would be helpful if I didn't need to use the IEntity2 interface, but thanks anyway!

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 28-Apr-2006 02:52:38   

Jeremy,

Why not make your childEntities's editorControls act like your entities's editorControls, making them to receive as parameter a entitiesCcollection. Normally the childentities's editor is a grid, then make your generic grid editor to receive the entitiesCollection.

Jeremy Driver avatar
Posts: 41
Joined: 08-May-2004
# Posted on: 02-May-2006 20:57:50   

Rogelio,

I'm not using many grids. Mostly, I use text boxes, combo boxes, etc. I can use a list box to contain child entities, when there is more than one child entity. Also, I'll commonly have a child that has a one to one relationship with its parent.

Jeremy

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-May-2006 14:21:27   

I don't know why you wanted to exclude Reflection in the first place, but I think you should be using it.