Database Structure to store multiple inheritance classes

拜拜、爱过 提交于 2019-12-13 02:43:59

问题


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


回答1:


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]



回答2:


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.



来源:https://stackoverflow.com/questions/1493088/database-structure-to-store-multiple-inheritance-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!