Trying to figure out the best way of processing a collection of TargetPerEntity objects. Here's my scenario:
*An employee has a collection of employee benefit "transactions"
*Transactions can either be health insurance, life insurance, or saving plan.
*Have the database setup as:
EMPLOYEE (table)
TRANSACTION (table)
TRANSACTION_HEALTH (table) (having details just for health)
TRANSACTION_LIFE (table) (having details just for life)
TRANSACTION_SAVINGPLAN (table) (having details just for savings)
*When I fetch an employee, I pre-fetch all the TRANSACTION records for that employee.
*For each TRANSACTION record, based on it's type (health, life, or savings), get the data for that type and paste it on a form.
*I could fetch each transaction and and do a try/cast to each possible (of the three) transaction types. When it doesn't try/cast fail...realize I got the right one and paste it on that particular form. But that seems kind of ugly.
Suggestion of a better way?
(HERE'S FROM THE HELP I WAS READING)
Polymorphic fetches
Already mentioned early in this section is the phenomenon called 'Polymorphic fetches'. Imagine the following entity setup: BoardMember entity has a relation (m:1) with CompanyCar. CompanyCar is the root of a TargetPerEntityHierarchy inheritance hierarchy and has two subtypes: FamilyCar and SportsCar. Because BoardMember has the relation with CompanyCar, a field called 'CompanyCar' is created in the BoardMember entity which is mapped onto the m:1 relation BoardMember - CompanyCar.
In the database, several BoardMember instances have been stored, as well as several different CompanyCar instances, of type FamilyCar or SportsCar. Using DataAccessAdapter.FetchNewEntity, you can load the related CompanyCar instance of a given BoardMember's instance by using the following code:
C# VB.NET // C#
CompanyCarEntity car = adapter.FetchNewEntity<CompanyCarEntity>(myBoardMember.GetRelationInfoCompanyCar());
' VB.NET
Dim car As CompanyCarEntity = adapter.FetchNewEntity(Of CompanyCarEntity)(myBoardMember.GetRelationInfoCompanyCar())
However, 'car' in the example above, can be of a different type. If for example the BoardMember instance in myBoardMember has a FamilyCar as company car set, 'car' is of type FamilyCar. Because the fetch action can result in multiple types, the fetch is called polymorphic. So, in our example, if 'car' is of type FamilyCar, the following code would also be correct:
C# VB.NET // C#
FamilyCarEntity car = adapter.FetchNewEntity<FamilyCarEntity>(myBoardMember.GetRelationInfoCompanyCar());
' VB.NET
Dim car As FamilyCarEntity = adapter.FetchNewEntity(Of FamilyCarEntity)(myBoardMember.GetRelationInfoCompanyCar()))
Would this BoardMember instance have a SportsCar set as company car, this code would fail at runtime with a specified cast not valid exception.