问题
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