Let’s say I have a simple application. It tries to add a new entry to a database grabbing the info from the user. The information is a typical MasterDetail object like an invoice.
- The Invoice object used to collect the data has some simple attributes (Id, Date, etc…) and a List of InvoiceDetail, consisting of some simple attributes (invoiceId, productId, quantity, etc…)
- A form in the UI Layer collects the data from the user, creates an instance of Invoice and sends it to the Business Layer. It has some simple controls for the simple atributes and a DataGridView for the InvoiceDetails list.
- The Business layer sends the data to a Validation Module. If any error in the input is detected it has to return some kind of error code.
- The Business Layer has to send the error code back to the Form to let the user correct the wrong information.
I want the application to display some kind of warning (a different background color for example) in every form control with an error.
- What’s a good approach to manage the errors to be able to inform the form which controls are wrong?
- Can I create an error object?
- Use delegates?
The harder problem I see is locating a specific item in the details since this list is are arbitrary long, but even without this problem I have no idea where to start.
I’m using VB.net but don’t think this is relevant to the solution of the problem.
P.D: Please suggest me some tags to this post.
If the error is to be notified to the user (so that the data in the form can be corrected) then the best way to notify the user is to use
exceptionrather than using cryptic error codes to denote the error.Suppose you have a method in the
Validationmodule that accepts the instance ofInvoice, then the code in theValidate()method should be something like this,Approach 1: Break when first encounter of the exception
and in the UI,
catchtheexceptionand handle it,The custom
InvoiceExceptioncan be created like this,And the
InvoiceDetailExceptionTypeenumeration is,Approach 2: Show cumulative exceptions
The entity classes,
The validator
The custom exceptions,
The exception type enumeration,
And finally the call to the validator,
More information