什么是hibernate实体和数据传输对象之间转换的好方法? [英] what is a good pattern for converting between hibernate entities and data transfer objects?

查看:237
本文介绍了什么是hibernate实体和数据传输对象之间转换的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的问题和关注,如何在Hibernate实体和数据传输对象之间转换由Web服务返回,如这个问题所讨论的:



在ejb3中使用数据传输对象被认为是最佳实践



这里提到的一个因素是,如果域模型发生变化,一组DTO将在Web服务的情况下保护消费者。 >

即使它似乎会在我的项目中添加大量的代码,这个推理看起来似乎是正确的。



是有一个很好的设计模式,我可以用来将一个Hibernate实体(实现一个接口)转换为实现相同接口的DTO?



所以假设以下两个实现书,我需要将BookEntity.class转换为BookDTO.class,以便我可以让JA XB序列化和返回。



再次,这整个前景对我来说似乎是可疑的,但是如果有好的模式帮助处理这个转换,我会喜欢得到一些洞察力。



有没有可能有一些有趣的方式通过反射转换?还是一个建设者模式,我不在想?



我应该忽略DTO模式并传递实体吗?

解决方案


我应该忽略DTO模式
并传递实体吗?




我的偏好通常是是。我不喜欢为了建筑或层次纯度而创建平行层次结构的想法。



当将实体EJB传递到视图层时,DTO模式的原始原因是EJB 1.0和2.0应用程序过于夸张。解决方案是将实体bean状态放入DTO。



通常为创建DTO而提供的另一个原因是禁止视图层进行修改。在这种情况下,DTO是不可变的对象,没有任何行为。我们会认为,DTO是一种核心的J2EE模式,成为一种反模式。



我意识到有些人会不同意。我只是提出我的意见这不是唯一的做法,也不一定是正确的方式。这是我的偏好。


I have had similar questions and concerns as to how to convert between Hibernate entities and data transfer objects to be returned by a web service as are discussed in this question:

Is using data transfer objects in ejb3 considered best practice

One of the factors mentioned here is that if the domain model changes, a set of DTOs will protect consumers in the case of a web service.

Even though it seems like it will add a substantial amount of code to my project, this reasoning seems sound.

Is there a good design pattern that I can use to convert a Hibernate entity (which implements an interface) to a DTO that implements the same interface?

So assuming both of the following implement 'Book', I would need to convert a BookEntity.class to a BookDTO.class so that I can let JAXB serialize and return.

Again, this whole prospect seems dubious to me, but if there are good patterns out there for helping to deal with this conversion, I would love to get some insight.

Is there perhaps some interesting way to convert via reflection? Or a 'builder' pattern that I'm not thinking of?

Should I just ignore the DTO pattern and pass entities around?

解决方案

Should I just ignore the DTO pattern and pass entities around?

My preference is usually "yes". I don't like the idea of parallel hierarchies created just for the sake of architectural or layer purity.

The original reason for the DTO pattern was excessive chattiness in EJB 1.0 and 2.0 apps when passing entity EJBs to the view tier. The solution was to put the entity bean state into a DTO.

Another reason that's usually given for creating DTOs is to prohibit modification by the view layer. DTOs are immutable objects in that case, with no behavior. They do nothing but ferry data to the view layer.

I would argue that DTO is a Core J2EE pattern that's become an anti-pattern.

I realize that some people would disagree. I'm simply offering my opinion. It's not the only way to do it, nor necessarily the "right" way. It's my preference.

这篇关于什么是hibernate实体和数据传输对象之间转换的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆