From d5de65fdb1802cdf498d65d93397f290813c377c Mon Sep 17 00:00:00 2001 From: muqiuhan Date: Tue, 9 Sep 2025 06:17:00 +0000 Subject: deploy: 9665097f0fa0dae9f92123fac54d26c0818758a5 --- .../index.html" | 1 - 1 file changed, 1 deletion(-) (limited to '2025/03/13') 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 35e8670e..4f96ab6d 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" @@ -207,7 +207,6 @@

用 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())
-

在这个例子中,Order 是聚合根,它通过 AddItem 方法来添加订单项,保证每个订单项符合业务规则。同时,聚合根 Order 还负责订单状态的管理,例如通过 ChangeStatus 方法来更新订单状态。OrderItem 是聚合内的一个实体,表示订单项,它通过 GetTotalPrice 方法来计算每个订单项的总价。外部系统只能通过 Order 聚合根来访问和操作订单项,而不能直接访问或修改 OrderItem

-- cgit v1.2.3