I have a Struts action controller which loads data from a view into the ActionForm using Hibernate. Is it really useful to use a DAO to decouple the data access from the controller?
If so would the ActionForm also serve as the DTO? If I have a separate DTO then how can I transfer the data from the DTO into the ActionForm? I know I could use reflection but all this seems unecessarily complex. Sorry I am new to Hibernate and J2EEpatterns so I'd appreciate any thoughts?
Having DAO separates the data access layer and isolates the changes to that particular layer. This is desirable.
Ideally, the ActionForm should only represent the attributes that are displayed in the html form. Your design must conform to this rule as much as possible.
The data transfer between the DTO and ActionForm can be accomplished by using the BeanUtils (reflection) utility class. In general it is not a good practice to use reflection. Main reason is that you cannot use debugger for troubleshooting problems in your code. Reflection is useful in developing tools that requires introspection.
Reflection complicates things unnecessarily. If you decide to use reflection mechanism such as BeanUtils be very careful, the class will silently fail to copy any properties that do not have the same names. The bugs can be very difficult to find. In practice it is just not worth using it in large applications. Whenever the requirements changes if you change either the DTO attributes or ActionForm attributes things can break. You have to manually make sure that they are consistent.
Another issue is that some of the attributes such as conversion from String to Date cannot be done by BeanUtils. You need to write transformation methods yourself.
Depending on the complexity of your application there are several ways that you move data from the presenation layer to the business layer. You could have an adapter that will do all the transformation from the ActionForm to the business layer objects. If the conversion is only for a few attributes then you could just have transformation methods defined in the ActionForm itself. If you are aware of the limitations of BeanUtils and if the attributes are such that it can benefit from it, you could just use it.