Many-to-any link back to parent

ActivRecord has a feature to create relations between tables with the name of the table as a variable.
The example deals with payment and order. A payment can be iDeal or PayPal. The dotNET code uses a interface (IPayment) which both classes implement.

public partial class Order
{
  [HasManyToAny(typeof(IPayment), "OrderId", "OrderPayments", typeof(Guid), "PaymentType", "PaymentId", MetaType = typeof(string))]
  [Any.MetaValue("iDeal", typeof(iDeal))]
  [Any.MetaValue("PayPal", typeof(PayPal))]
  public virtual IList<IPayment> Payments { get; set; }
}

In the database a table is created with fields OrderId, PaymentType and PaymentId. OrderId contains the key of the Order table, PaymentId contains the key of the referenced table and PaymentType contains the name of the referenced table. (iDeal or PayPal)
Now I need a link from Payment to Order. But no examples shows me that implementation.
Looking at it functionaly: it should be the same relation but the other way around, where the type always is the actual type of the class we are starting from.

public partial class iDeal : IPayment
{
  [HasManyToAny(typeof(Order), "OrderId", "OrderPayments", typeof(Guid), "PaymentType", "PaymentId", MetaType = typeof(string))]
  [Any.MetaValue("iDeal", typeof(Order))] // PaymentType should always be iDeal 
  public virtual IList<Order> Orders { get; set; }
}

Now create a property that reads and writes the first element of the Orders list and the many-to-any link back to the parent is created.

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development, Tooling. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s