存储多个继承类的数据库结构
我有两个不同的(无继承)接口:IInvestable
和 IPricable
和两个类:IndexClass
和 FundClass
它们存储在我的数据库中的单独表中。
IndexClass
是 IPriceable
FundClass
是 IPriceable
和 IInvestable
。
我需要一个表来存储 IndexClass
和 FundClass
的价格
我需要一个表来存储投资的 FundClasses
。
一种方法可能是有两个表:Pricable
和 Investable
,它们只有一个 ID柱子。
然后在 IndexClass
上有一个外键:PricableID
FundClass
上还有两个外键:PricableID
和 InvestableID
。
然后,我的价格只需链接到 PricableID
,我的投资只需链接到 InvestmentID
,因为它们是唯一的。
或者,我可以在价格表上添加 FundClassID
和 IndexClassID
列,但这似乎不可扩展,并且似乎很难使用。
然后让 NHibernate 解决它——我仍然不能 100% 确定它会喜欢!
我在互联网上找不到任何关于存储多重继承的建议,想知道是否有人对此有任何想法,或者他们过去是否解决过类似的问题?
谢谢斯图
I have two distinct (no inheritance) interfaces:IInvestable
and IPricable
and two classes:IndexClass
and FundClass
that are stored in seperate tables in my DB.
IndexClass
is IPriceable
FundClass
is IPriceable
and IInvestable
.
I need a table to store prices against an IndexClass
and a FundClass
I need a table to store that FundClasses
are invested in.
One way might be to have two tables: Pricable
and Investable
which just have an ID column.
Then have a foreign key on IndexClass
: PricableID
Also have two foreign keys on FundClass
: PricableID
and InvestableID
.
Then my Prices simply link to the PricableID
and my Investments simply link to the InvestmentID
as these will be unique.
Alternativly I could have FundClassID
and IndexClassID
columns on the Price table but this doesn't seem scalable and seems like it'll be difficult to work with.
Then let NHibernate figure it out - which I'm still not 100% certain it'll like!
I can't really find anything on the internet with recommendations for storing Multiple Inheritance and wondered if anyone had any ideas for this or if they've tackled something similar in the past?
Thanks
Stu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,我更倾向于组合而不是继承。例如,这些类可以是 PriceInfo 和 InvestmentInfo,并且您的 IInvestable 和 IPricable 接口将包含这些类型的属性。
编辑:生成的数据库架构将如下所示:
In this case, I would favor composition over inheritance. The classes could be PriceInfo and InvestmentInfo, for example, and your IInvestable and IPricable interfaces would include properties of those types.
Edit: The resulting database schema would look something like this:
所以我最终得到了以下结构:
FundClass: FundClassID (etc)
FundClassReturn:FundClassID, ReturnID
IndexClass:IndexClassID(等)
IndexClassReturn: IndexClassID, ReturnID
返回:ReturnID(等)
然后我只需在 NHibernate 映射文件中对其进行排序。我试图变得过于老式并使其过于以数据库为中心。无论如何,我拥有的所有内容都已写入接口,因此我只需查询接口并让 NHibernate 使用内在的多态性处理其余部分。效果就像一个魅力!
不过,感谢您的帮助 - 它引发了对最终解决方案的思考。
So I ended up with the following structure:
FundClass: FundClassID (etc)
FundClassReturn:FundClassID, ReturnID
IndexClass: IndexClassID (etc)
IndexClassReturn: IndexClassID, ReturnID
Return: ReturnID (etc)
then I simply sort this in the NHibernate mapping file. I was trying to be too old fashioned and making it too DB centric. Everything I have is written to interfaces anyway so I simply query the interfaces and let NHibernate handle the rest with intrisic polymorphism. Works like a charm!
Thanks for your help though - it's provoked the thoughts for the end solution.