Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java DDD中域对象集合的最佳实践_Java_Design Patterns_Domain Driven Design - Fatal编程技术网

Java DDD中域对象集合的最佳实践

Java DDD中域对象集合的最佳实践,java,design-patterns,domain-driven-design,Java,Design Patterns,Domain Driven Design,我有以下课程: public class DomainClass { private Integer value; private Integer total; private Integer month; public Double getPercent(){/*Business logic*/} } 我想对DomainClass对象列表执行相同的getPercent操作,无需重复代码。我有两个想法来处理这个问题,但我不知道它们是否好 想法1-创建服务并迭代列

我有以下课程:

public class DomainClass {
    private Integer value;
    private Integer total;
    private Integer month;
    public Double getPercent(){/*Business logic*/}
}
我想对DomainClass对象列表执行相同的getPercent操作,无需重复代码。我有两个想法来处理这个问题,但我不知道它们是否好

想法1-创建服务并迭代列表:

public class DomainObjectService{
....
public Double getPercent(List<DomainClass> list){
    double value, total;
    for(DomainClass d : list){
        value += d.getValue();
        total += d.getTotal();
    }
    // do percent operation
}
我读到的是,在DDD中,实体应该处理自己的逻辑,但在这个收集操作的例子中,应该如何处理它


好吧,如果我的DDD基础知识是错误的。我想知道java中DDD的好文章/书籍/示例。

您如何管理您的实体?你用过ORM吗


我对此类操作的解决方案是构建一个类来管理对象集合。 例如:

public class DomainClasses{
   private final List<DomainClass> domainClasses;

   ....

   public Double getPercent(){
      // manage the percent operation   ...
      // ... on all the members the way ... 
      // ... your business is expected  ...
      // ... to do it on the collection

      return something;
   }

   // class initialization
}
公共类DomainClasses{
私有最终列表域类;
....
公共双百分比(){
//管理百分比操作。。。
//…一路上所有的成员。。。
//…你的生意很好。。。
//…在收藏中使用
归还某物;
}
//类初始化
}
通过这种方式,您可以重用每个类的getPercent代码,但也可以实现集合使用的特定版本。此外,集合可以访问DomainClass的包private getter(如果有)来进行此计算。通过这种方式,您只公开构建域对象所需的函数

注意:如果您在没有任何ORM的情况下管理持久性,那么这个解决方案是可行的。或者,如果您想使用它,则需要额外的工作来正确配置容器类

一些链接:

(我使用与PM分离的DM)

(这就是我正在做的,将域对象转换为将被持久化的DTO——这需要为集合编写一点额外的代码,但一旦测试成功,它就会一直工作,并且您得到的是一个不受ORM框架干扰的域)


问题后更新。我使用这种模式

存储

我的域类有一个函数,可以导出Memento对象中的所有数据。存储库获取一个域实例,请求提供纪念品,然后:

  • 我生成SQL插入/更新(来自Spring的带有事务管理的纯SQL)
  • 您可以加载您的JPA实体并使用Memento信息进行更新(应该小心,但如果您编写测试,一旦完成,它将始终有效-因此,测试非常重要;)
阅读

相反,从保存的数据构建域实例时,我执行以下操作:

  • 在实现存储库代码的持久层中,我扩展了Memento(我们称之为persistedmento)
  • 当我必须加载某些内容时,我会构建一个persistedMento,并使用它来构建域类的实例
  • 我的域类有一个函数,允许从Memento构建对象。注意:这并不总是必要的,但在我的例子中,主构造函数有额外的检查,当从保存的对象重建对象时,无法执行这些检查。无论如何,这简化了域类的重建
要防止域类在域外部使用,请执行以下操作:

  • 我的存储库需要一个存在的事务,因此它们不能直接用于代码中的任何地方
  • Memento类具有受保护的构造函数,因此它们只能在域包或存储库包中使用。PersistedMemento也隐藏在存储库包中,因此无法创建任何实例
注释


当然,这不是完美的解决方案。Domain类有两个函数,用于支持非域需求。Memento类也可以是子类,并且可以使用实例来构建域类(但是为什么呢?使用默认构造函数构建域类要简单得多)。但是,除了这少量的污染外,域保持真正干净,我可以真正专注于域需求,而不考虑如何管理持久性

你好,卢卡。我正在使用JPA。你能给我一个例子,说明如何从ORMPojo映射到域对象吗?因为现在我在下午做事情。
public class DomainClasses{
   private final List<DomainClass> domainClasses;

   ....

   public Double getPercent(){
      // manage the percent operation   ...
      // ... on all the members the way ... 
      // ... your business is expected  ...
      // ... to do it on the collection

      return something;
   }

   // class initialization
}