Polymorphic fetches collections

Posts   
 
    
dals
User
Posts: 17
Joined: 10-Jul-2006
# Posted on: 27-Sep-2006 05:41:49   

Hi,

I have the follow data structure:



CREATE TABLE Payment (
       AccountID            varchar(20) NOT NULL,
       PaymentID            uniqueidentifier NOT NULL,
       InsertTimeStamp    datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
       UpdateTimeStamp    datetime NULL,
       OperationDate        datetime NOT NULL
...
)

ALTER TABLE Payment
       ADD PRIMARY KEY NONCLUSTERED (AccountID, PaymentID)
go


CREATE TABLE PaymentCreditCard (
       AccountID            varchar(20) NOT NULL,
       PaymentID            uniqueidentifier NOT NULL,
...
)

ALTER TABLE PaymentCreditCard
       ADD PRIMARY KEY (AccountID, PaymentID)
go

CREATE TABLE PaymentPayPal (
       AccountID            varchar(20) NOT NULL,
       PaymentID            uniqueidentifier NOT NULL,
 ....
)

ALTER TABLE PaymentPayPal
       ADD PRIMARY KEY (AccountID, PaymentID)
go


ALTER TABLE PaymentPayPal
       ADD FOREIGN KEY (AccountID, PaymentID)
                             REFERENCES Payment
go

ALTER TABLE PaymentCreditCard
       ADD FOREIGN KEY (AccountID, PaymentID)
                             REFERENCES Payment
go


So, basically I have a Payment table and its specializations (PaymentCreditCard and PaymentPayPal).

What I want to do is inform a OperationDate and receive a EntityCollection of Payments. This means that if a Payment is a credit card payment, i should receive a PaymentCreditCardEntity type for a given item on the EntityCollection<PaymentEntity>. If a Payment is a pay pal payment, I should receive a PaymentPayPalEntity type for a given item on the EntityCollection<PaymentEntity>. And so on.... so on the same collection i can have all this payments types, since all of them are types of PaymentEntity.

I'm NOT using the discriminator since the LLBLGen create this sub-types automatically when I choose "Construct Target-per-Entity Hierarchies". I do have this information on Payment table (PaymentTypeID), but I'm not able to use it (what I think it would be nice to have this mix of techniques, but this is another discussion)

Thank you,

David

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 27-Sep-2006 07:19:42   
// C#, .NET 2.0
CustomerEntity customer = new CustomerEntity("CHOPS");
DataAccessAdapter adapter = new DataAccessAdapter();
EntityCollection<OrderEntity> orders = customer.Orders;
adapter.FetchEntityCollection(orders, customer.GetRelationInfoOrders());

If Order is in an inheritance hierarchy, the fetch is polymorphic. This means that if the customer entity, in this case customer "CHOPS", has references to instances of different derived types of Order, every instance in customer.Orders is of the type it represents, which effectively means that not every instance in Orders is of the same type. See for more information about polymorphic fetchs also Polymorphic fetches.

Is this not the behavior that you are experiencing? Can you post the code that is giving you problems?

dals
User
Posts: 17
Joined: 10-Jul-2006
# Posted on: 27-Sep-2006 18:47:56   

bclubb wrote:

Is this not the behavior that you are experiencing? Can you post the code that is giving you problems?

I think it worked:


IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.PaymentEntity);

EntityCollection payments = new EntityCollection(new PaymentEntityFactory());

IRelationPredicateBucket filter = new RelationPredicateBucket();

filter.PredicateExpression.Add(PaymentFields.AccountId == account.AccountId);

using (DataAccessAdapter adapter = new DataAccessAdapter())
{
            adapter.FetchEntityCollection(payments, filter, prefetchPath);

}

return payments;

What I'm trying to figure out now is how to do it with a single Payment entity, like FetchEntity.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 28-Sep-2006 03:37:11   

Take a look at the section about Polymorphic Fetches under Using the generated code - Adapter - Using the entity classes. FetchNewEntity can be used for polymorphic fetches.