继承和 Dapper.net
我决定使用 Dapper.net,因为它似乎只做我想要的事情:映射,我不需要任何花哨的东西,我只是无聊地处理我的数据读取器和我的对象之间的映射。
我的问题:
假设我有这些类:
class Foo{
int ID;
string Name;
}
class Bar : Foo{
string FavoriteMoovie;
}
和这些表:
Foo
- ID
- Name
Bar
- FooID
- FavoriteMoovie
所以我想知道如何在同一查询中选择我的 Foo 和 Bars ?
到目前为止,我唯一的想法是
- 选择所有不是 Bars 的 Foos
- ,然后选择所有 Bars
我不能使用“Query”方法的重载,因为这里只是用于映射关系。
I decided to use Dapper.net because it seems to be doing only what I want : mapping, I don't need anything fancy , I'm just bored to handle the mapping between my datareader and my object.
My problem :
Let's say I have these class :
class Foo{
int ID;
string Name;
}
class Bar : Foo{
string FavoriteMoovie;
}
And these tables :
Foo
- ID
- Name
Bar
- FooID
- FavoriteMoovie
So I'd like to know how I can select my Foo and Bars in the same query ?
My only idea so far is
- SELECT all the Foos that are not Bars
- then SELECT all the Bars
I can't use the overload of the method "Query" because there are just here for mapping relationships.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
非常hacky,但这可能有效。
缺点是它创建了一些不需要的中间对象。
其他选项是简单地选择
Bar
对象,然后使用相同的技巧来过滤掉 Foos 或选择一个动态,然后转换为正确的类。就我个人而言,我不会仅仅为了处理这个问题而查询两次,除非您允许
NULL
FavoriteMoovie,在这种情况下您别无选择,只能使查询复杂化或选择两次。Very hacky, but this probably works.
Disadvantage is that it creates a couple of intermediate objects that it could do without.
Other options are to simply select
Bar
objects and then use the same trick to filter out the Foos or select a dynamic and then convert to the correct class.Personally I would not query twice just to handle this, unless you are allowing for
NULL
FavoriteMoovie, in which case you have no choice but to either complicate the query or select twice.在每个层次结构 TPH 一个表的情况下,很容易获得所有具体的子类,特别是如果 BaseClass 是抽象的
并且存储库从 TPH 表中选择:
in the case when one table per hierarchy TPH, it is easy to get all concrete childs Class, especially if the BaseClass is abstract
and Repository select from TPH table: