If this is in the wrong forum, please point me to the right one.
I'm considering using Spring's validation framework at my service layer, instead of throwing exceptions. I like the idea because it allows multiple exception cases to be reported at once, and because of the Errors class's support for resolvable error codes.
e.g. my methods are going to be
instead ofCode:doJob(Object argument, Errors errors)
But I have two questions:Code:doJob(Object arguments) throws JobException
- Has this been proven to work in real life? The validation class javadoc keeps referring to web objects, like "usually your form controller does this for you", so has the validation been proven for usability without web dependencies? Does it scale, such that other machine clients (as opposed to web users) can run the methods efficiently?
- How does Spring suggest I create an Errors instance, when calling these methods? Is there anything cleaner than:
And is it really a good idea to make the consumer do that work? Does it tend to work better if the service method creates its own Errors object (and then, probably, wraps it in an Exception and throws it)?Code:Greeting greeting = new Greeting("hello"); DataBinder dataBinder = new DataBinder(greeting); Errors errors = dataBinder.getBindingResult(); doJob(greeting, errors);
If anyone has already gone down this road, I would very much appreciate the wisdom of experience.
Thanks,
- Travis


Reply With Quote
What are your choices with the AOP approach? I see two, either throwing an unchecked (otherwise you'll get a UndeclaredThrowableException) exception with the complete validation results (Errors object) as parameter or storing the validation results in a ThreadLocal validation context from where you can access it from what you call consumer.