Java - 处理层次结构
你好: 我有一个应用程序,其中线程层次结构(持久实体)建模如下(请注意,这可能是深度嵌套的层次结构):
Thread
{
private key;
private rootKey;
private parentKey;
.. getters ..
.. setters ..
}
我有一个具有以下结构的 DTO,
ThreadDTO
{
private key;
private rootKey;
private parentKey;
ArrayList<ThreadDTO> childThreads;
... getters ...
... setters ...
}
我想将实体实例转换为 DTO。是否有任何标准算法/最佳实践可以最有效地用于执行此转换? 任何反馈将不胜感激..
Hello:
I have an app where in a thread hierarchy (persisted entity) is modelled as follows (note that this could be a deeply nested hierarchy):
Thread
{
private key;
private rootKey;
private parentKey;
.. getters ..
.. setters ..
}
I have a DTO which has the following structure
ThreadDTO
{
private key;
private rootKey;
private parentKey;
ArrayList<ThreadDTO> childThreads;
... getters ...
... setters ...
}
I would like to convert the entity instances into the DTO. Are there any standard algorithms / best practices available that can be optimally be used for doing this transformation ?
Any feedback would be appreciated..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DTO 非常烦人,你能用它们做的最好的事情就是创建一个方法,该方法在参数中接受一个 Thread 并复制属性,另一个方法将返回一个 Thread 。带有属性的副本。
Adam Bien 的另一个解决方案是 通用 DTO,安全性较低但也减少了代码的复制/粘贴。
DTO are quite annoying, the best thing you can do with them is to create a method which takes a
Thread
in parameter and copy the attributes, and another which will return aThread
with a copy of the attributes.An other solution from Adam Bien is the Generic DTO, there is less security but also less copy/paste of in code.