public class Customer : XPObject
{
public XPCollection Orders { get; }
}
public class Order : XPObject
{
}
However, as XPO currently requires you to implement a two-way association in your object model. You have to write something like this:
public class Customer : XPObject
{
[Association("CustomerOrders")]
public XPCollection Orders { get; }
}
public class Order : XPObject
{
[Association("CustomerOrders")]
public Customer Customer;
}
XPO has made me:
- Add an attribute to my association property.
- Give that attribute a unique string to identify the association.
- Add a new property to my linked type.
- Add an attribute to this new property.
- Give this attribute the same string so as to identify it is part of the association.
You are probably looking at the above example and thinking that you would naturally have an association from order to customer, and I would agree. However, let's say that you have a requirement to show the recently ordered products against the customer. One way of implementing this would be:
public class Customer : XPObject
{
public XPCollection RecentProducts { get; }
}
In this circumstance, I would not want to have to implement a reverse association:
public class Customer : XPObject
{
[Association("CustomerRecentProducts")]
public XPCollection RecentProducts { get; }
}
public class Product : XPObject
{
[Association("CustomerRecentProducts")]
public XPCollection Customers { get; }
}
In this case I would consider the reverse association to be worse than code noise - it is structurally worng and simply should not be there! Maybe we should be suspicious of the Customer.RecentProducts association in the first place too, but that's another story!
Another time when being forced to implement a reverse association is when the two classes are in different assemblies. I would have to move the classes to avoind a circular reference. Maybe I should ask for XPO to support persistence by interface too...
No comments:
Post a Comment