Solution: Pass an error collection as a return object from service methods.
Problem: Error collection prevents more logical objects from being returned, as an error holds no true value in the domain context.
Solution: Create error collection during the client request that is passed as a parameter to service methods.
Problem: Client has to instantiate Errors collection. Also, the client is not forced to handle any broken business rules that may have occurred. Thus rendering validation efforts futile.
Final Solution: Return Errors only from "validate" methods. Have your service methods throw a runtime exception, of type ValidationException. This ValidationException should persist an immutable collection of errors.
Caveat: Every service call will need to individually catch validation exceptions.
class Errors implements iterable {
...
public void evaluate() {
if(this.hasErrors)
throw ValidationException(this);
}
}
class StuffService {
...
Stuff findStuff(StuffCriteria criteria) {
Errors errors = stuffValidator.validate(criteria);
errors.evaluate(); //throws ValidationException if errors present
return stuffDao.findStuff(criteria);
}
}
class StuffClientSearchForm {
...
onSubmit() {
...
try{
stuff1 = stuffService.findStuff(criteria1);
} catch (ValidationException ve) {
for(Error error in ve.getErrors())
addClientError(error.getMessage);
}
try{
stuff2 = stuffService.findStuff(criteria2);
} catch (ValidationException ve) {
for(Error error in ve.getErrors())
addClientError(error.getMessage);
}
...
}
}
No comments:
Post a Comment