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

Working in a DevOps team is the best thing that happened to me. 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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.