In my app, I am creating a series of validation classes to check, for example, that a user entered Name property of the class (from a WinForm) doesn't exceed the size of the varchar() in the database.
Currently, the validation code will throw a custom Exception if the Name field is too large. (The custom exception, when caught by the UI, will show the custom exception message in a MessageBox as opposed to my generic error form for regular exceptions.) Validation classes are in the app's class library, and are scoped as Friend. The flow looks like this:
- The DLL's Public Service Layer used by the WinForms --(calls)--> Friend Validation Layer
- The DLL's Public Service Layer used by the WinForms --(calls)--> Friend Data Access Layer if validation succeeds.
Simplified example:
Public Shared Sub CreateCustomer(cust as Customer)
Validation.Customer.ValidateForCreate(cust) ' scoped as Friend
Dal.Customer.Create(cust) ' scoped as Friend
End Sub
Is it "smart" design to have the Validation Layer throw custom exceptions back to the UI when validation fails?
Is it better design to just return True/False from the Validation Layer along with a String message of what failed, and have the Service Layer handle throwing exceptions?
Is it better design to just return True/False from the Validation Layer, and have the Services Layer bubble up the True/False to the UI along with a String message of what failed?
I'm trying to keep an object oriented approach. My opinion is that throwing custom exceptions doesn't break OOP principles, but I'd like other opinions :)