DTO 的父/子模型
我不确定这种情况是否更多地与泛型相关,而不是DTO,但事情是这样的:
我有一个代表一个人的DTO 。 人可以拥有其他人作为子,或者只是这些人的ResourceLink强>(s)。这意味着子级可以是以下 2 种类型之一:Person(DTO)或ResourceLink。它是什么类型是通过 queryParam 确定的,并因此在逻辑上通过随后的流程确定。我想仅使用 ONE 集合对象来表示它们,但不知道这样做的最佳方法。
这是我到目前为止所得到的:
public class PersonDTO<T> {
@XmlElementWrapper(name = "children")
@XmlElement(name = "child")
List<T> children;
// other stuff
}
使用这种方法,我需要根据 if...else
条件定义翻译类型。
早些时候,我有 2 个不同的集合,其中一个仍然是 NULL。我还考虑过在新的 DTO 中提取 关系 作为 ChildrenDTO (不确定这是否是一个好主意)
我想知道是否有针对这种情况的最佳实践,否则,如果可以根据条件声明 PersonDTO
或 PersonDTO
。
提前致谢!
I am not sure if this situation would more be related to generics than DTOs, but here it goes:
I have a DTO that represents a Person. A Person can have as children other Person(s) or just ResourceLink to those Person(s). This means that the child can be of either of the 2 types: Person (the DTO) or the ResourceLink. What type it would be, is determined through a queryParam and consequently logically through the flow follwed. I want to represent them using just ONE collection object and am not aware of the best way to do so.
Here is what I have so far:
public class PersonDTO<T> {
@XmlElementWrapper(name = "children")
@XmlElement(name = "child")
List<T> children;
// other stuff
}
With this approach, I need to define the translated type based on an if...else
condition.
Earlier I had 2 different collections, one of which remained NULL. I also thought of extracting the relationship thing out in a new DTO as ChildrenDTO (not sure if that's a great idea)
I would like to know if there is a best practice for this situation, otherwise, if it is possible to declare a PersonDTO<PersonDTO>
or PersonDTO<ResourceLink>
depending on a condition.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议改为对 List 子项的元素使用第三种类型:
I'd suggest instead, using a third type for the elements of List children: