Adapter TargetPerEntity polymorphic fetches of collection

Posts   
 
    
greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 07-Mar-2014 00:07:41   

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.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Mar-2014 07:09:21   

Hi Andy,

What are you actually trying and what is the problem you get? Please post more context info (LLBLGen version, RTL, repro code, expected results, etc): http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7725

David Elizondo | LLBLGen Support Team
greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 07-Mar-2014 13:40:50   

Hi Daelmo,

I guess I actually forgot that part of the posting. confused

For each employee (who has a collection of transactions...which could be health, life, or savings), we have a page that lists the transactions he/she has done.

The employee should be able to click on any of those transactions and it will pull data from the "transaction" / "transaction_health", "transaction_life", or "transaction_savings" and post it on a pdf form that is popped into a window.

When I do a fetch from the employee, I can do a pre-fetch path for the "transaction" table/entity (which is a child to the "employee" table). However, since the "transaction" and the "transaction_health", "transaction_life", or "transaction_savings" tables are in a target-per-entity structure (as there is alot of common data in the "transaction" table), I can't get the data from health, life, or savings.

My question is: From the list of transactions, what is the best way to fetch the particular entity in it's fully resolved (meaning the data from, for example, the transaction -with- transaction_health) form?

I don't have an "error" per say right now. I am trying to figure out how to do this.

Kind regards,

Andy

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 07-Mar-2014 14:44:14   

I believe the fetch is polymorphic. i.e. Please inspect the generated prefetch SQL query, and inspect the type of the fetched entities.