views:

49

answers:

1

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 message)
    • Data

Here are the properties of each individual BillItem (some are common):

alt text

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.

+2  A: 

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.

Anurag