Re: Automated Validation of Attributes

From: Mike Kienenberger (mkienen..mail.com)
Date: Wed Mar 10 2010 - 14:01:04 EST

  • Next message: Michael Gentry: "Re: Automated Validation of Attributes"

    Instead of reflection in the cayenne core, have you considered
    changing your entity template setters? Perhaps it could be provided
    as an alternate WebObjects-esque template set.

    public void setFoo(Foo newFool)
    {
         if (validateFoo(newFoo))
         {
             writeProperty("foo", newFoo);
         }
    }

    public boolean validateFoo(Foo newFoo)
    {
        // do nothing by default
        return true;
    }

    I don't remember the exact syntax for WebObjects or for Cayenne, but
    the above example conveys the general idea.

    On Tue, Mar 9, 2010 at 10:42 PM, Andrew Lindesay <ap..indesay.co.nz> wrote:
    > Hello;
    >
    > As you may know, WebObjects/EOF has the protocol;
    >
    >        NSValidation
    >
    > Specifically, this protocol implements the following two methods;
    >
    >        Object validateTakeValueForKeyPath(Object value, String keyPath)
    >        Object validateValueForKey(Object value, String key)
    >
    > The effect is that when a client tries to write a property "foo", it will ultimately try to invoke;
    >
    >        Object validateFoo(Object value) throws NSValidation.ValidationException
    >
    > ...before...
    >
    >        setFoo(Object value)
    >
    > ...in order to either "repair" the value before assigning or to throw an exception.  This is very handy from the user-interface perspective.  For each attribute of an entity, this technique is also automatically used in the case of "validateForSave()" which means that aside from inter-attribute validation, per-attribute validations can be implemented just once.
    >
    > To provide this same behaviour to Cayenne, I would like to put forward the idea to implement something similar;
    >
    >        validatedWriteProperty(ValidationResult vr, String propName, Object value)
    >        validatedWriteNestedProperty(ValidationResult vr, String propName, Object value)
    >
    > This would use (cached) reflection to look for a "validate" method and fire that before writing the property.  For each attribute of a DataObject's entity, these methods would be fired from;
    >
    >        void validateForSave(ValidationResult validationResult)
    >
    > Does this make sense in the context of Cayenne?
    >
    > Regards;
    >
    > ___
    > Andrew Lindesay
    > www.lindesay.co.nz
    >
    >



    This archive was generated by hypermail 2.0.0 : Wed Mar 10 2010 - 14:01:58 EST