
Originally Posted by
bpolka
your last question is quite different one from what was asked originally and should probably be addresses on the architecture forum.
But here are a few thoguhts:
Your business layer should throw 2 kinds of exceptions, checked and unchecked.
If your BL is throwing a checked exception, it usually means that the user is capable of fixing the error by providing different parameters on the request (hopefully thru a form you designed). In this case you should be able to report to the user what was the problem and redisplay the form for resubmission.
If your BL throws the runtime exception (unchecked), there is really nothing you can do about ... from your presentation layer developer's perspective. So, just design an exception page and inform the user of the fact that there is something wrong with the system they should try again later or relogin and try again the same operation or design your own way of dealing with the situation.
Now back to your question about BL validation. We have to look here at the kinds of validations you would regularly perform on your presentation layer (PL) and on your business layer. If you think about it, it is quite obvious they are very different.
PL validations are lightweight (by this I mean they usually need no db connection, no server-side resources, etc.) and they can very often be performed on the client side alltogether, without the need for roundtrip to the server. Theese are validations for required fields and dates being in an acceptable format, and required fields if another field is specified, etc. That is the reason you see examples of them, that seemingly not giving you the real life examples. They actually are real life but only for PL of your system. Now if you turn to the BL of your system that is what's usuall missing from the examples or maybe not emphasized enough.
BL validation is different, because you really need to make sure that for example you can add a new item to the collection that is owned by some containing entity. Another good example would be to check for the validity of the email address by contacting the mail server of the destination email server and asking if the email is valid (note: this will not always work, because some servers are not online all the time :-)). Contrast this with the validation of the email by simply looking for the @ sign to be there and ending in (.com, .edu ...), which is obviously a PL validation. BL can validate against simple stuff just like the PL did, but not neccesarily so, since if you have your BL collocated with your PL you will know for sure that the PL already did the validation so you can skip it in BL. However, if the BL is accessible by different PLs (think of web PL, rich client PL or some B2B agent accessing your business layer), you can not trust the validity of the data sent to you (think of the B2B agent), you will have to include the simple validation in your BL too. This is where optimization you talked about (propagating the validations to the BL) would be usefull.
So, to wrap up the post it basically all boils down to what is the architecture of your system. Where each of the parts are and how much you can trust the agent that is accessing you BL. My guess is that the majority of the projects is probably falling into the category of collocated PL and BL so no duplication of validations needed, and spring is pretty good at this kind of stuff. If you have a different design you will need to think of what are the validation that you will have to perform and where to place your optimizations
HTH