I am looking for a good approach to validation of Entity Bean attributes. DTO's are typically used for wrapping data on the client side and transmitting to server. Although some level of validation could be done in the DTO object in the web container (to facilitate quicker client response), I must assume that this validation (together with more complex validation) would have to be repeeated on the EJB container side. Is this an unavoidable thing with validation, or is there a neater way of doing validation? Also, from a pure OO perspective, is it not better to perform the validation in the bean itself, in which case some validation would be duplicated in the bean and in the DTO object?
I have not really seen validation been strongly discussed in any of the J2EE best practices patterns. Any thoughts here would be appreciated.
1. JB dat
hey i think DTO is a kinda useless in a web environment if u don't do any data validation. Just think of how many round trips you need to go for sending the DTO to the EJB layer for data validation in an example of user login.
checking some simple data format and string length shouldn't be done in the EJB layer.
for most of the important attributes within a DTO, i will try to add a "isXXXOK" methods other than "setXXX/getXXX".
This "isXXXOK" methods basically will do some basic data validation (e.g. email address format..)
if things go wrong here, you can notify the user before sending anything to the EJB layer. Within the EJB layer, if you want to have a solid round check, you can first call these "isXXXOk" method and then do some other critical data validation.
by doing that you can separate some data validations depending on how deep you want go for. The simple stuff stay within the DTO, and while the heavy one stays within the EJB layer..
that's my perspective...
(p.s. by DTO, i mean a regular jsp JavaBean).
in struts framework these responsibility lies on formBeans - they provide the validation and initialisation logic for web forms. You do not use them to transfer data, thus keeping the DTO as simple small and efficient struct-like objects.