简体   繁体   English

Grails域设计:继承还是具有可空属性的一类?

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

I have a domain OrderItem 我有一个域OrderItem

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

class Product {
 //other details
}

class Packaging {
//other details
}

Currently, OrderItem refers only to Beers . 当前,OrderItem仅指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. 我认为这些都是OrderItem对象,但是系统当前将OrderItem视为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. 仍将OrderItem仅视为Beers。 Then all the new items will have their own class that extends OrderItem. 然后,所有新项目将具有扩展OrderItem的自己的类。 Pros and cons? 利弊? Table per hierarchy or per class? 表是按层次结构还是按类?
  2. Add properties(some are nullable) to OrderItem to distinguish the new items? 在OrderItem中添加属性(某些属性可以为空)以区分新项? Like 'category', 'someItemCode', 'someItemPricing', etc. 像“类别”,“ someItemCode”,“ someItemPricing”等。

I prefer the second one, adding additional property like category or itemType . 我喜欢第二个, 添加其他属性,例如categoryitemType

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 ). 因此,在添加新字段时,迁移脚本必须为所有现有记录将itemType字段值设置为Beers (因为您拥有的所有产品都是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. 我不知道您的哪个字段将为null ,但是如果您的OrderItem表仍然具有许多nullable列,则需要对其进行规范化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM