【发布时间】:2015-07-21 20:15:38
【问题描述】:
我有一种方法可以在两个不同类的对象之间进行转换。 这些是 DTO 对象和休眠实体类。
public static DomainObject1 convertToDomain(PersistedObject1 pObj) {
if (pObj == null)
return null;
DomainObject1 dObj = new DomainObject1();
BeanUtils.copyProperties(pObj,dObj); //Copy the property values of the given source bean into the target bean.
return dObj;
}
而不是使用与DomainObject2 和PersistedObject2 等相同的方法.. 是否可以使用具有以下签名的通用方法?(无需传递源类和目标类)
public static<U,V> U convertToDomain(V pObj) {
...}
PS:(一个different 主题,当实体具有相同的结构而有些人不同意尽管休眠文档和其他来源时,使用 DTO 是一种浪费)
【问题讨论】: