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.