代码之家  ›  专栏  ›  技术社区  ›  x__dos

在进行域驱动开发时对数据进行排序

  •  0
  • x__dos  · 技术社区  · 15 年前

    在开发数据模型时,我没有注意底层数据库级别。我在域类的属性中封装了一些逻辑(例如:计算订单利润的方法)。在我需要整理数据之前一切都很好。排序是一个纯粹的数据库特性(主要是由于性能原因,我无法从表中检索所有行并在域级别对它们进行排序)。
    结果我不得不重复逻辑,以前在我的领域级封装在数据库级(不仅重复,而且翻译成数据库语言)。
    这看起来很糟糕,我不想做。
    有没有办法绕过这样的问题?

    编辑

    我的域:

    class Order {
      var lines;
    
      profit() {
        return lines.sum(line => line.sellPrice * line.quantity) - 
          lines.takeAllOutcomesAndSum(outcome => outcome.buyPrice * outcome.quantity)
      } 
    }
    
    class Line {
      var outcomes;
      var sellPrice
    }
    
    class Outcome {
      var buyPrice;
      var quantity;
    }
    


    实际上,我想到了在数据库中存储计算值,而不是计算它们

    2 回复  |  直到 15 年前
        1
  •  0
  •   Arnis Lapsa    15 年前

    而不是这个:

    class Order {
      lines;
    
      profit() {
        return lines.sum(line => line.sellPrice * line.quantity) - 
          lines.takeAllOutcomesAndSum(outcome => outcome.buyPrice * outcome.quantity)
      } 
    }
    
    class Line { outcomes; sellPrice }    
    class Outcome { buyPrice; quantity; }
    

    class Order {
      lines; profit;
      void Order(lines){
        profit = lines.sum(line => line.sellPrice * line.quantity) - 
          lines.takeAllOutcomesAndSum(outcome => outcome.buyPrice * outcome.quantity)
      }
    }
    
    class Line { outcomes; sellPrice; }    
    class Outcome { buyPrice; quantity; }
    

    只要确保它保持有效(每次order.lines更改时都更新它),就应该没问题。

        2
  •  0
  •   Stephan Eggermont    15 年前

    一般的解决方案是在域级别声明您需要的内容,并从中生成相关的数据库代码。