[Figleaf-developer] Validation and PropertyDescriptor
Status: Alpha
Brought to you by:
steckman
|
From: <sam...@ma...> - 2004-07-06 12:07:04
|
In the light of my work on Validation, I think we need to re-evaluate the PropertyDescriptor, at least the way in which it exposes the read and write methods. If we take the view that Validation is used to verify whether or not a value is valid for a domain object, then it is fair to say the validation is business logic. Currently, to invoke a property, you call one of these two methods on PropertyDescriptor: Method getReadMethod(); Method getWriteMethod(); I dislike this for a variety of reasons: 1. It exposes an implementation detail - what if I want my property's setter to call several methods for example? 2. It completely bypasses all validation logic that might apply to a property - you would have to hope that any UI layer validates before calling read or write, resulting in the developer not being able to guarantee that their (business) validation logic would ever be invoked. 3. Calling a property becomes more complex than needed I prefer the following solution - we add the following method to PropertyDescriptor: /** * Invokes validation without setting the value - useful in UI's to provide * immediate feedback to the user of invalid values etc. */ boolean isValid(Object newValue, ValidationCallback callback); Next, get rid of getReadMethod and getWriteMethod and instead use: Object read(); /** * Sets a new value if valid, else callback.invalid() will be called */ void write(Object newValue, ValidationCallback callback); For this PropertyDescriptor would have to become a per-instance object (which I don't think is a big problem). If we wanted to keep PropertyDescriptor as per-class, then we'd need to pass in an additional parameter into read/write. Also note that there is now no need to expose ValidationRules outside of the PropertyDescriptor, helping keep the responsibility for determining an Objects validity inside the Object itself. sam |