存储多个继承类的数据库结构

发布于 2024-08-05 22:39:50 字数 970 浏览 2 评论 0原文

我有两个不同的(无继承)接口:
IInvestableIPricable
和两个类:
IndexClassFundClass
它们存储在我的数据库中的单独表中。

IndexClassIPriceable
FundClassIPriceableIInvestable

我需要一个表来存储 IndexClassFundClass 的价格
我需要一个表来存储投资的 FundClasses

一种方法可能是有两个表:PricableInvestable,它们只有一个 ID柱子。
然后在 IndexClass 上有一个外键:PricableID
FundClass 上还有两个外键:PricableIDInvestableID

然后,我的价格只需链接到 PricableID,我的投资只需链接到 InvestmentID,因为它们是唯一的。

或者,我可以在价格表上添加 FundClassIDIndexClassID 列,但这似乎不可扩展,并且似乎很难使用。

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

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

发布评论

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

评论(2

东走西顾 2024-08-12 22:39:50

在这种情况下,我更倾向于组合而不是继承。例如,这些类可以是 PriceInfo 和 InvestmentInfo,并且您的 IInvestable 和 IPricable 接口将包含这些类型的属性。

编辑:生成的数据库架构将如下所示:

PriceInfo
---------
PriceInfoId
[Other Stuff]

InvestmentInfo
--------------
InvestmentInfoId
[Other Stuff]

IndexClass
---------
IndexClassId
PriceInfoId
[Other Stuff]

FundClass
---------
FundClassId
PriceInfoId
InvestmentInfoId
[Other Stuff]

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:

PriceInfo
---------
PriceInfoId
[Other Stuff]

InvestmentInfo
--------------
InvestmentInfoId
[Other Stuff]

IndexClass
---------
IndexClassId
PriceInfoId
[Other Stuff]

FundClass
---------
FundClassId
PriceInfoId
InvestmentInfoId
[Other Stuff]
听风念你 2024-08-12 22:39:50

所以我最终得到了以下结构:

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.

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