DTO 的父/子模型

发布于 2024-12-21 19:32:24 字数 923 浏览 2 评论 0原文

我不确定这种情况是否更多地与泛型相关,而不是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 (不确定这是否是一个好主意)

我想知道是否有针对这种情况的最佳实践,否则,如果可以根据条件声明 PersonDTOPersonDTO

提前致谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2024-12-28 19:32:24

我建议改为对 List 子项的元素使用第三种类型:

    public interface PersonResolver () {
          Person resolvePerson ();
    }

    public class Person implements PersonResolver {
          Person resolvePerson () { return this; }
    }

    public class ResourceLink implements PersonResolver {
          Person resolvePerson () {
               if (myLinkTargetType == TARGET_TYPE_PERSON)
                      { return (Person) myTarget; }
               return null;
          }
    }

I'd suggest instead, using a third type for the elements of List children:

    public interface PersonResolver () {
          Person resolvePerson ();
    }

    public class Person implements PersonResolver {
          Person resolvePerson () { return this; }
    }

    public class ResourceLink implements PersonResolver {
          Person resolvePerson () {
               if (myLinkTargetType == TARGET_TYPE_PERSON)
                      { return (Person) myTarget; }
               return null;
          }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文