From 4da1dc74954fafe4d21841892bafc955a37ac805 Mon Sep 17 00:00:00 2001 From: muqiuhan Date: Tue, 27 May 2025 06:06:45 +0000 Subject: deploy: 74a1d57ead41454de540829cd6de5ba1f5702bd6 --- .../index.html" | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to '2025/03/13/领域驱动设计中的“聚合根”/index.html') diff --git "a/2025/03/13/\351\242\206\345\237\237\351\251\261\345\212\250\350\256\276\350\256\241\344\270\255\347\232\204\342\200\234\350\201\232\345\220\210\346\240\271\342\200\235/index.html" "b/2025/03/13/\351\242\206\345\237\237\351\251\261\345\212\250\350\256\276\350\256\241\344\270\255\347\232\204\342\200\234\350\201\232\345\220\210\346\240\271\342\200\235/index.html" index 56df60c9..be156f43 100644 --- "a/2025/03/13/\351\242\206\345\237\237\351\251\261\345\212\250\350\256\276\350\256\241\344\270\255\347\232\204\342\200\234\350\201\232\345\220\210\346\240\271\342\200\235/index.html" +++ "b/2025/03/13/\351\242\206\345\237\237\351\251\261\345\212\250\350\256\276\350\256\241\344\270\255\347\232\204\342\200\234\350\201\232\345\220\210\346\240\271\342\200\235/index.html" @@ -195,15 +195,15 @@

在领域驱动设计(Domain-Driven Design,简称DDD)中,聚合根(Aggregate Root)是聚合(Aggregate)中的核心实体,是一个聚合的入口点和控制者,负责维护聚合内部的一致性和不变性条件。聚合是一组紧密相关的领域对象的集合,这些对象通过一定的业务规则绑定在一起,并被视为一个单元。

主要的作用如下:

其具有以下特性:

用 F# 来描述,以订单管理为例,大概写一下:

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
31
32
33
34
35
36
37
38
39
type OrderStatus = 
| New
| Shipped
| Delivered
| Cancelled

type OrderItem (productName: string, price: float, quantity: int) =
do
if quantity <= 0 then
failwith "Quantity must be positive"

member public this.ProductName = productName
member public this.Price = price
member public this.Quantity = quantity
member public this.TotalPrice () = price * quantity

type Order (id: int, customerName: string) =
let mutable status = OrderStatus.New
let mutable orderItems = []

member public this.Id = id
member public this.CustomerName = customerName
member public this.Status = status
member public this.OrderItems = orderItems

member public this.AddItem (item: OrderItem, price: float, quantity: int) =
if quantity <= 0 then
failwith "Quantity must be positive"

orderItems <- orderItems @ [OrderItem(item.ProductName, price, quantity)]

member public this.ChangeStatus (status: OrderStatus) =
this.Status <- status

member public this.TotalPrice () =
orderItems |> List.sumBy (fun item -> item.TotalPrice())

member public this.GetTotalPrice () =
orderItems |> List.sumBy (fun item -> item.TotalPrice())
-- cgit v1.2.3