Problem

One of the options is to pass the values from "Form Bean" to the session bean methods in the form of method arguments. This does not have any technical flaw except that on change of number of arguments we have to change the method definition too, and possibly if there is some interface definition, even that would require a change. The suggested solution is to copy the data into a DTO and pass the same to EJB-tier. Ideally this DTO shall be passed to session bean (session façade), which in turn can talk to Entity beans.

Best Practice

There are two options available for populating Data Transfer Object

   It has been observed that developers frequently use the 1st option and there is no issue with that except that this mundane exercise is repeated every time besides making the code very bulky and ugly. While copying, suitable data type conversion has to be hand-coded in the DTO setter methods, so that when data is copied from the Form-Bean to the DTO, it gets converted to appropriate business type. For example, a Form bean can capture all the client layer attributes as a String but they need to be converted to various Java or native type before performing any business operation on them.

  

You can save yourself from hand-coding all this by using copyProperties() method of BeanUtils class This copies the data from one bean to the another provided the variable names of business attributes in "Value or Transfer Object" is same as the one in Form bean. This also relieves you from data-type conversion by transparently converting source-bean attribute type to destination-bean attribute type.

--Puneet Agarwal


Comments

One can use XDoclet to generate "value objects" (DTOs) from his entity bean's classes. But one must be careful, because this approach hurts every time an entity bean definition changes... ;o)

--Carsten Habicht

StrutsCatalogDTOImplementation (last edited 2009-09-20 23:12:18 by localhost)