I was thinking about how validation is applied in different scenarios and especially using layered architecture and I came across some issues that I would like to discuss.
Are there validations that should be used at the application level rather than the business level? If they exist, then they should not be forced on the entities but on the application layer above.
When we find validation errors how should we propagate these errors to the upper layers?
1) Create a specific type of Exception (EValidationException)? And from this could the higher layers create code to trap and display error messages?
try
customerService.registerCostumer(name, phoneNumber, email);
except
on E:EValidationException do
Self.ShowValidationMessages(E.getErrorMessages());
end;
2) Create an Application Context type that would be injected into Application Services and serve as a bridge to validation errors?
validator := TValidator.Create;
//in service layer
if validator.isValid(customer) then
FApplicationContext.addMessages(validator.getErrorMessages());
//in presentation layer (a form for example)
if applicatioContext.hasErrorMessages() then
Self.ShowValidationMessages(applicationContext.getErrorMessages());
I'm going to point out that I'm developing in Delphi and my project uses a layered architecture like DDD layers (I'm not using DDD 100% yet, I just use some ideas). In this case I'm calling my validation code in the application services.