如何使用 Dapper 有效地选择聚合对象?
假设我有一系列形成聚合的对象。
public class C{
public string Details {get;set;}
}
public class B{
public string Details {get;set;}
public List<C> Items {get;set;}
}
public class A{
public long ID {get;set;}
public string Details {get;set;}
public List<B> Items {get;set;}
}
使用 Dapper,从数据库中的表填充这些内容的最佳方法是什么(在我的例子中是 postgres,但这并不重要)。示例中的表与对象模型几乎是一一对应的。类上的 Items 属性表示与每个从属对象的外键关系。即 3 个表,A 与 B 具有一对多关系,B 与 C 具有一对多关系。
因此,对于给定的 AI ID,希望我的对象也拥有其所有子数据。
我最好的猜测是我应该以某种方式使用 QueryMultiple,但我不确定如何最好地做到这一点。
Lets say that I have a series of objects that form an aggregate.
public class C{
public string Details {get;set;}
}
public class B{
public string Details {get;set;}
public List<C> Items {get;set;}
}
public class A{
public long ID {get;set;}
public string Details {get;set;}
public List<B> Items {get;set;}
}
using Dapper, what is the best way to populate these from tables in a database (in my case it's postgres but that shouldn't matter). The tables in the example are pretty much one for one with the object model. The Items property on the class representing foreign key relationships to each subordinate object. i.e. 3 tables, A has a One to Many relationship with B, B has a One to Many relationship with C.
So for a given ID of A I want my objects to have all their child data as well.
My best guess is that I should use QueryMultiple somehow but I am not sure how best to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为我在这里建议的助手: Multi-Mapper to创建对象层次结构可能会有所帮助。
假设您扩展 GridReader 并使用映射器:
您可以扩展此模式以使用 3 级层次结构。
I think the helper I propose here: Multi-Mapper to create object hierarchy may be of help.
Assuming you extend your GridReader and with a mapper:
You could extend this pattern to work with a 3 level hierarchy.