Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by Alan Fishe » Fri, 29 Jul 2005 08:55:46


I am building a stateless distributed web application which has validation
of information performed in many layers. Most of the validation will be
repeated in both the UI and business layers as the code is structured to
also expose functionality as a web service. More advanced validation will be
done in both the business layer and data layer e.g. validating groups of
related properties or related information in rows of data

When validating the information for 'anticipated' problems (errors and
warnings), I want to collect all of the various errors/warnings and report
them back to the UI as some sort of collection. Ideally, I would prefer not
to pass the collection between methods so was looking at possibly using a
singleton but I would have to somehow isolate the collection per user's
session since the app is 'mostly' stateless.

I have searched the net and found many great articles on Exception
Management but nothing detailed on validation. Can anyone recommend an
approach or point me to some articles?

TIA
Alan
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by lukezha » Fri, 29 Jul 2005 11:20:57

Hello Alan,

If you don't want pass the exceptions between methods, another way may be
MSMQ. Your web service can put the exception information in MSMQ, and
client UI receives them. I suggest you may take a look at LOGGING
Application Block, it support log information in MSMQ:

http://www.yqcomputer.com/
/logging.asp?frame=true

hope this help,

Luke

 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by Alan Fishe » Fri, 29 Jul 2005 12:23:10

Hi Luke,

Thanks for the reply. Maybe I didnt explain myself properly. I am not
talking about exceptions as I am already using the application blocks for
these. I am talking about validation messages (errors and warnings). These
are expected errors where usually, if one occurs, you want to collect it and
continue, collecting all of the errors (throughout the layers) until the
processing ends. At the end, the UI reports all of the messages so the user
can take action.

Exceptions are different because they stop processing (unless they are
handled) as a result of an exceptional circumstance

Thanks
Alan
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by pete » Sat, 30 Jul 2005 02:15:31

Hi Alan,

I set the Exception's Message property by appending to what's already
there, and then re-throw the exception.

HTH

Peter
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by Alan Fishe » Sat, 30 Jul 2005 07:32:58

Thanks Peter, but I am not talking about exceptions. I am talking about
non-trivial validation errors which are 'expected'. e.g. data was
incorrectly entered with validation rules performed via a stored procedure
detecting the 'error'

Alan
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by lukezha » Sat, 30 Jul 2005 11:27:08

Hi Alan,

I think this won't conflict with my suggestion. When your web service find
an error/warning, it can create some log information and post them to MSMQ,
then continue to handle the information. It won't stop the processs. When
it finished and returned to clients, clients can receive all logged
infornmation from MSMQ.

Luke
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by Nigel Norr » Sat, 30 Jul 2005 19:25:56

A couple of possibilities come to mind:

A) Depending on how you pass data around between your layers, hang the error
collection off the data. DataSet already provides places to store errors,
and also a generic mechanism to hang other objects onto the DataSet.

B) Use thread local storage for your 'singleton'. In the outermost call you
clear the errors collection at the start, and return the collection to the
remote caller when done. This depends on all your methods for a single
request being called on the same thread - if you are using something that
may get called on a different thread, then it gets more complex.

For more discussion of some of this, see the Registry pattern in Martin
Fowler's book:

http://www.yqcomputer.com/

HTH

--------
Nigel Norris
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by Jay B. Har » Sun, 31 Jul 2005 03:25:41

Alan,
Rockford Lhotka's CSLA .NET uses a "Broken Rules" collection that represents
each of the validation failures for a domain object.

http://www.yqcomputer.com/

Unfortunately his site appears to be down temporarily...

Hope this helps
Jay



|I am building a stateless distributed web application which has validation
| of information performed in many layers. Most of the validation will be
| repeated in both the UI and business layers as the code is structured to
| also expose functionality as a web service. More advanced validation will
be
| done in both the business layer and data layer e.g. validating groups of
| related properties or related information in rows of data
|
| When validating the information for 'anticipated' problems (errors and
| warnings), I want to collect all of the various errors/warnings and report
| them back to the UI as some sort of collection. Ideally, I would prefer
not
| to pass the collection between methods so was looking at possibly using a
| singleton but I would have to somehow isolate the collection per user's
| session since the app is 'mostly' stateless.
|
| I have searched the net and found many great articles on Exception
| Management but nothing detailed on validation. Can anyone recommend an
| approach or point me to some articles?
|
| TIA
| Alan
|
|
|
|
|
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by pete » Sun, 31 Jul 2005 18:43:22

K. I can only offer our experience, I'm afraid - so I don't know how
relevant it is.

For business rules (i.e. is the input valid for our business? - e.g. a
student number must be 8 digits, etc), we construct a remote validation
class that lives on the application server. It has methods to validate
all data and process all business rules.

When a user moves from one page to another of the application, we apply
the rules to the data in the page/form in order to decided whether the
user should be allowed to progress, or carry out the requested action.

Normally, we would return on the first failure and report that, but I
can see there would be cases for applying all the rules and reporting
all the failures. We would do this by calling all the appropriate
methods in the remote validation object, building up a message to return
as failures occur.

You could either return a null string on success, with the error message
(as built) as the return value on failure or, as we prefer, to return
true on success and throw an exception with the error message assigned
to the exception's message property, on failure. This latter means that
you must configure remoting to return error messages from remote
exceptions, of course, in your config file.

HTH

Peter

Alan Fisher wrote:
 
 
 

Collecting and propogating Validation Errors and Warnings in the Business Layer and Data Layer

Post by Albert Tol » Wed, 03 Aug 2005 18:37:36

I'm having the same problem. I have created a class ValidationResult which
looks like:
class ValidationResult
{
ValidationStatus Status;
ValidationItemCollection Items;
}
The ValidationItem is defined as below:
class ValidationItem
{
ValidationStatus Staus;
string Code; // Error code usually, but can be a warning, info or sth
else
string Message;
}
ValidationStatus is an enumeration which for the moment is defined as:
enum ValidationStatus
{
Success,
Failed
// Later can be added Warning, Info etc.
}
Now the problem is how to pass the ValidationResult object to the UI. I see
3 options:
1. throw an exeception which has a property of type ValidationResult. The UI
then can catch the exception and proceed. Looks a bit messy to me...
Pros: Force the UI to handel the validation.
Cons: Looks like a bad design. UI code would be messy...
2. Fire an event on a "global" object accessible from both UI and
components. "Recomment" the UI to attach an event handler...
Pros: UI code should be better organized...
Cons: UI is not forced to handle the event...
3. Use some kind of queue to add the ValidationResult objects from the
components and poll them from the UI.
Pros: ?
Cons: ?

Any thoughts are welcome.

Thanx,
Albert