好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用Spring Data JDBC实现DDD聚合的示例代码

本文讨论了spring data jdbc如何实现ddd中聚合根存储的设计思路,其中主要讨论了是不是每个实体都需要一个对应数据表,这种问题需要根据具体情况而定。

spring data jdbc比jpa更容易理解,比如对象引用特性会很有趣。作为第一个示例,请考虑以下领域模型:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

class purchaseorder {

 

  private @id long id;

  private string shippingaddress;

  private set<orderitem> items = new hashset<>();

 

  void additem( int quantity, string product) {

   items.add(createorderitem(quantity, product));

  }

 

  private orderitem createorderitem( int quantity, string product) {

 

   orderitem item = new orderitem();

   item.product = product;

   item.quantity = quantity;

   return item;

  }

}

class orderitem {

  int quantity;

  string product;

}

另外,考虑如下定义的存储库:

?

1

2

3

4

5

interface orderrepository extends crudrepository<purchaseorder, long > {

 

  @query ( "select count(*) from order_item" )

  int countitems();

}

如果使用商品创建订单,希望所有商品都能保存:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@autowired orderrepository repository;

 

@test

public void createupdatedeleteorder() {

 

  purchaseorder order = new purchaseorder();

  order.additem( 4 , "captain future comet lego set" );

  order.additem( 2 , "cute blue angler fish plush toy" );

 

  purchaseorder saved = repository.save(order);

 

  assertthat(repository.count()).isequalto( 1 );

  assertthat(repository.countitems()).isequalto( 2 );

  …

此外,如果删除purchaseorder,它的所有项目也应该被删除。

?

1

2

3

4

5

6

  repository.delete(saved);

 

  assertthat(repository.count()).isequalto( 0 );

  assertthat(repository.countitems()).isequalto( 0 );

}

如果我们需要一个语法上相同但语义上不同的关系呢?上述订单中包含订单条目orderitem , 当订单删除时,包含的orderitem 都删除了,但是看看看看下面案例,也是使用包含一个集合:

?

1

2

3

4

class book {

  // …

  set<author> authors = new hashset<>();

}

当书籍绝版时,将book删除。所有的作者author也都丢失了。这当然不是你想要的,因为一些作者可能也写过其他书。

怎么办?

让我们看一看存储库实际存在的内容。这与一遍又一遍的问题密切相关:是否应该在jpa中为每个表创建一个存储库?

而正确和权威的答案是[不]。存储库持久聚合并加载聚合。聚合是一个包含各种对象的群,它应始终保持一致。此外,它应始终保持(和加载)在一起。它有一个对象,称为聚合根,它是唯一允许外部访问或引用聚合内部的代理或管理者。聚合根是传递给存储库的,以便持久化聚合里面的对象群。

这提出了一个问题:spring data jdbc如何确定什么是聚合的一部分,哪些不是?答案非常简单:非瞬态non-transient 引用都是聚合的一部分,这样就可从聚合根到达聚合内部所有内容。

orderitem实例是聚合的一部分,因此被删除; author正好相反,实例不是book聚合的一部分,因此不应删除。所以不应该从book内部去引用那些作者author对象。

问题解决了。好吧,......不是真的。我们仍然需要存储和访问有关book和author之间的关系信息。答案可以在领域驱动设计(ddd)中找到,它建议使用id而不是直接引用。这适用于各种多对x关系。

如果多个聚合引用同一个实体,则该实体不能成为引用它的多个聚合的一部分,因为它只能是其中一个聚合的一部分。因此,任何[多对一]和[多对多]关系都只能通过引用id来建模实现了。

这样可以实现多种目的:

1. 清楚地表示了聚合的边界。

2. 还完全解耦(至少在应用程序的领域模型中)所涉及的两个聚合。

3. 这种分离可以用不同的方式在数据库中表示:

a. 以通常的方式保留数据库,包括所有外键。这意味着必须确保以正确的顺序创建和保留聚合。

b. 使用延迟约束,仅在事务的提交阶段进行检查。这可能会提高吞吐量。它还编纂了最终一致性的版本,其中[最终]与交易结束相关联。这也允许引用从未存在的聚合,只要它仅在事务期间发生。这对于避免大量基础结构代码只是为了满足外键和非空约束可能是有用的。

c. 完全删除外键,实现真正的最终一致性。

d. 将引用的聚合保留在不同的数据库中,甚至可能是no sql存储。

无论如何,即使spring data jdbc也鼓励应用模块化。此外,如果尝试迁移一个具有10年历史的单体,你就会明白它的价值。

使用spring data jdbc,您可以建模多对多关系,如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

class book {

 

  private @id long id;

  private string title;

  private set<authorref> authors = new hashset<>();

 

  public void addauthor(author author) {

   authors.add(createauthorref(author));

  }

 

  private authorref createauthorref(author author) {

 

   assert .notnull(author, "author must not be null" );

   assert .notnull(author.id, "author id, must not be null" );

 

   authorref authorref = new authorref();

   authorref.authorid = author.id;

   return authorref;

  }

}

 

@table ( "book_author" )

class authorref {

  long authorid ;

}

 

class author {

  @id long id;

  string name;

}

请注意额外的类:authorref,它表示有关某个作者的book聚合的知识。它可能包含有关作者的其他聚合信息,然后实际上会在数据库中重复。考虑到author数据库可能与book数据库完全不同,这会产生很多问题。

另请注意,authors是book 私有字段,authorref实例化在私有方法createauthorref中发生。因此聚合之外的任何内容都不能直接访问它。spring data jdbc绝不需要这样做,但ddd鼓励这么做。

下面是测试:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@test

public void booksandauthors() {

 

  author author = new author();

  author.name = "greg l. turnquist" ;

 

  author = authors.save(author);

 

  book book = new book();

  book.title = "spring boot" ;

  book.addauthor(author);

 

  books.save(book);

 

  books.deleteall();

 

  assertthat(authors.count()).isequalto( 1 );

}

上述完成了我们设想功能:删除书籍后,并没有将书籍作者数据表数据全部删除,虽然作者是书籍的一个私有字段。

总结一下:

spring data jdbc不支持多对一或多对多关系。要对这些进行建模,请使用id。

这鼓励了领域模型的清晰模块化。

通过类似的思路,避免双向依赖。聚合内部的引用是从聚合根到元素。聚合之间的引用使用只在一个关联方向上使用id表示。此外,如果需要反向导航,请在存储库中使用查询方法。这样能清楚确定哪个聚合负责维护引用。

banq注:是不是每个实体都需要一个对应数据表?根据具体情况,order和orderitem之间生命周期是一致的,删除订单,订单条目也没有存在意义;而book和author则不是生命周期一致的,book可能是当前有界上下文的聚合根,而author是另外一个有界上下文如作者管理系统的聚合根,如果删除book同时,也将author删除,其实是不符合要求的,这时候应该将author作为值对象看待,author的id就是一个值,然后建立一个类authorref ,包含这个值,作为被book引用的对象,这样就不是整个author实体聚合对象被book引用了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://www.jdon.com/50192

查看更多关于使用Spring Data JDBC实现DDD聚合的示例代码的详细内容...

  阅读:13次