XPCollection peculiarity

These days I’ve came across this situation – related to [DevEx]’ XPO:

This piece of code creates an instance of generic XPCollection that deals with Customer list. Next I set the condition to use for data retrieval and at the end I say that it is OK to load the data (note that XPCollection does lazy load). However it won’t work like that. Second line will throw an exception: can’t reload collection with disabled loading. The solution is quite simple but perhaps it won’t work always for you – just switch second and third row to get code like this:

If you ignore the fact that it doesn’t allow you setting the condition before loading (unless you do it in constructor), which I struggle to understand why, you’ll see another issue. The guidelines for creating properties say something like (out of my memory):
It shouldn’t matter the order of setting properties
Properties shouldn’t perform any actions and should return the same value when called multiple times in a row

Clearly the way that XPCollection works is against first rule but not against second rule as one would assume from the exception text as it really doesn’t perform any action when setting Criteria – it just throws an exception if LoadingEnabled is disabled. I hope that this behavior will change in the future as it is against guidelines and it doesn’t allow you to construct Criteria before loading is enabled.

Leave a Reply