one issue regarding using Polymorphic association

Jan 24, 2013 at 12:32 PM

I have a scenario like this. I have a class called PaymentItem, it contains two PAs: 

One is PaymentItemPayeeLink class which link payment item to payee.

The other one is PaymentItemSourceLink which link payment item to Source. 

The issue is when I display payment item on the screen, the payee property of paymentItem can be resolved(i.e. displayed) on the screen. However, the PaymentSource property can't be resolved. Then I discovered that this was caused by not including PaymentItemSourceLink in my linq query to retrieve the payment items related to the payee. The below is code snippet. 

public virtual List FindItemsAwaitingPayment(IPayee payee)
            var payeeType = payee.GetType().ToString().Split('.').Last().Split('_').First();

            //search for all payment items linked to payee
		    var paymentItemPayeeLinks = Container.Instances();
		    var paymentItems = Container.Instances();
		    var paymentItemSourceLinks = Container.Instances();

		    var itemsAwaitingPayment = from paymentItem in paymentItems
		                               join paymentItemPayeeLink in paymentItemPayeeLinks on paymentItem.Id equals
                                       join p in paymentItemSourceLinks on paymentItem.Id equals p.Owner.Id
		                                   paymentItem.Payment == null
                                           && paymentItemPayeeLink.AssociatedRoleObjectId == payee.Id
                                           && paymentItemPayeeLink.AssociatedRoleObjectType.Contains(payeeType)
		                               select paymentItem;

            return itemsAwaitingPayment.ToList();


However this time, it returned 0 item which should not be. Then i check the values in paymentItemSourceLinks variable. It contains 3 items which is correct because there are three records in the table like below. However two of them contains incorrect value for Owner property, the values are null.  But the owner(i.e. PaymentItem) value should not be null. I wonder if you have come across the same issue.  

Id payment_source_type       payment_source_id Owner_Id

1 Sdm.Cluster.Payments.Impl.ExternalPaymentSource 1 2

2 Sdm.Cluster.Payments.Impl.ExternalPaymentSource 1 8

3 Sdm.Cluster.Payments.Impl.ExternalPaymentSource 1 9

Jan 24, 2013 at 1:32 PM

You should be using the PolymorphicNavigator#FindOwners method.  That will return you all the PaymentItems for the payee as an IQueryable<PaymentItem>, which you can then filter down to find the unpaid ones.

BTW, your method is returning a List.  Don't throw away the type information unnecessarily:  at least return IList<PaymentItem>.  But you might even be better off returning IQueryable<PaymentItem> anyway, because then whatever is calling it can perform further filtering without unnecessary resolution of unneeded instances. 

Jan 24, 2013 at 1:54 PM

Thanks very much for pointing out the direction, Richard!