从多个查询填充 DTO

发布于 2024-09-26 09:03:18 字数 614 浏览 1 评论 0原文

我有一个具有 40 多个属性的 DTO。但为了填充所有属性,我需要执行 4 个单独的查询。我的第一个查询负责获取基本信息。对于返回的每一行,我根据主查询给出的 id 再运行 3 个查询(N+1 问题)。我可以设置使用急切加载,但随后我加载了数千个我不需要的对象。

我是否应该拆分我的 DTO 并为我运行的每个查询创建一个单独的 DTO,然后链接然后通过 id 将它们全部绑定到一个中央 DTO 中?

我正在设想这样的最终 DTO。

public class FooDto
{
    public string Foo { get; set; }
    public string Bar { get; set; }
    public FirstDto FirstQueryResults { get; set; }
    public SecondDto SecondQueryResults { get; set; }
    public ThirdDto ThirdQueryResults { get; set; }
}

有更好的方法来解决这个问题吗?我正在使用 Oracle,NHibernate 不支持多条件。请注意,我正在加入大部分数据。当我需要使用一组全新的条件查询数据时,问题就出现了。

I have a DTO with 40+ properties. But in order to populate all properties I need to execute 4 separate queries. My first query is in charged of getting basic information. For each row returned I run 3 more queries based on the id's given from the main query (N+1 problem). I can set use eager loading but then I'm loading thousands of objects that I don't need.

Should I split my DTO and create a separate DTO for each query I run then link then tie them all together into a central DTO by id?

I was envisioning a final DTO like this.

public class FooDto
{
    public string Foo { get; set; }
    public string Bar { get; set; }
    public FirstDto FirstQueryResults { get; set; }
    public SecondDto SecondQueryResults { get; set; }
    public ThirdDto ThirdQueryResults { get; set; }
}

Is there a better way of solving this? I'm using Oracle and NHibernate doesn't support multi criterias. Note that I am joining most of my data. The problem comes when I need to query data with a complete new set of criteria.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

丑丑阿 2024-10-03 09:03:18

创建一个视图来连接数据以一次性提供 40 个属性,并将您的 DTO 建立在该视图之上 - 无论 DTO 是什么;-)

How about creating a VIEW that joins the data to give the 40 properties all in one go, and basing your DTO on that - whatever a DTO may be ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文