DTO pattern: Best way to copy properties between two objects

后端 未结 6 1307
死守一世寂寞
死守一世寂寞 2020-12-12 12:33

In my application\'s architecture I usually send the object or list of objects from the data access layer to the web layer via the service layer, in which these objects get

6条回答
  •  爱一瞬间的悲伤
    2020-12-12 13:07

    You can use Apache Commmons Beanutils. The API is

    org.apache.commons.beanutils.PropertyUtilsBean.copyProperties(Object dest, Object orig).

    It copies property values from the "origin" bean to the "destination" bean for all cases where the property names are the same.

    Now I am going to off topic. Using DTO is mostly considered an anti-pattern in EJB3. If your DTO and your domain objects are very alike, there is really no need to duplicate codes. DTO still has merits, especially for saving network bandwidth when remote access is involved. I do not have details about your application architecture, but if the layers you talked about are logical layers and does not cross network, I do not see the need for DTO.

提交回复
热议问题