Search code examples
grailsinheritancegrails-orm

Grails Domain Design: inheritance or one class with nullable properties?


I have a domain OrderItem

class OrderItem {
    Product product
    Packaging packaging
    //other details
}

class Product {
 //other details
}

class Packaging {
//other details
}

Currently, OrderItem refers only to Beers. Product is the type of beers. Packaging is the cans/bottles/keg/etc.

Then the client decides to also sell items that are not beers: Spirits, Ingredients, Souvenirs, etc.

I'm thinking that these are all OrderItem objects, but the system currently treats OrderItem as Beers. How should I introduce the expanded models? (Note: Some of the new items don't have product, packaging or both.)

  1. Inheritance? Still treat OrderItem as Beers only. Then all the new items will have their own class that extends OrderItem. Pros and cons? Table per hierarchy or per class?
  2. Add properties(some are nullable) to OrderItem to distinguish the new items? Like 'category', 'someItemCode', 'someItemPricing', etc.

Solution

  • I prefer the second one, adding additional property like category or itemType.

    So when adding the new field your migration script must set the itemType field value to Beers for all the existing records( Because all the products you had are all Beers).

    I dont know which of your fields are going to be null, but if your OrderItem table is still having many nullable columns you need to to normalize it.