Sometimes you have a list of objects (say List<PurchaseOrder>) and you want to get all associated objects (say OrderLines) for all the orders in the list. There are 2 ways of doing this in ECO
- You call a PersistenceService method which takes the list of orders and a string identifying the association name. I don’t like this approach because the name of the association may change during modelling and this wont be picked up as an error until runtime.
- You use the overloaded method which takes an IAssociationEnd. I don’t like this either because you have to find the IAssociationEnd instance from the meta-model at runtime, and to do this you’d probably use a name anyway.
So, what’s the alternative?
public static void EnsureRelatedObjects<T>(this IPersistenceService instance, IEnumerable<T> objects, Expression<Func<T, object>> member) where T : IEcoObject
MemberExpression memberExpression = (MemberExpression)member.Body;
Now you can write code like this
EcoSpace.Persistence.EnsureRelatedObjects(orders, o => o.Lines);
EcoSpace.Persistence.EnsureRelatedObjects(orders, o => o.Customer);
And it will be checked at compile time too.