Modeling a cellphone bill: should I use single-table inheritance or polymorphic associations?
In my domain:
- Users have many Bills
- Bills have many BillItems (and therefore Users have many BillItems through Bills)
- Every BillItem is one of:
- Call
- SMS (text message)
- MMS (multimedia me开发者_运维技巧ssage)
- Data
Here are the properties of each individual BillItem (some are common):
alt text http://dl.dropbox.com/u/2792776/screenshots/2010-04-13_2146-1.png
My question is whether I should model this arrangement with single-table inheritance (i.e., one "bill_items" table with a "type" column) or polymorphism (separate tables for each BillItem type), and why.
I would go with a Polymorphic association as there are enough fields already that don't apply to all/most of the items. STI will just waste a lot of space, but ignoring optimization, it's also a very rigid design as the most natural way to extend on that design when more fields are required will be to add them to the table.
Polymorphic association on the other hand only specifies a contract that all implementors must follow. In this case the contract only says that an item must be billable, and allows each individual type of item to evolve independently. If there is logic to be shared among these different classes, it might be better to just convert it into a module and include that.
精彩评论