8000 demonstrate how to use ISpecification · dddplus/dddplus-demo@6e8cd9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e8cd9a

Browse files
committed
demonstrate how to use ISpecification
1 parent 06efb97 commit 6e8cd9a

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ curl -XPOST http://localhost:9090/reload?plugin=isv # plugin hot reloading
5252
- [前台对中台的步骤编排](order-center-bp-ka/src/main/java/org/example/bp/oms/ka/extension/DecideStepsExt.java)
5353
- [动态的步骤编排](order-center-cp/cp-oc-domain/src/main/java/org/example/cp/oms/domain/step/submitorder/BasicStep.java)
5454
- [扩展属性通过扩展点的实现](order-center-bp-isv/src/main/java/org/example/bp/oms/isv/extension/CustomModelExt.java)
55+
- [业务约束规则的显式化](order-center-cp/cp-oc-domain/src/main/java/org/example/cp/oms/domain/specification/ProductNotEmptySpec.java)
56+
- [如何使用](order-center-cp/cp-oc-domain/src/main/java/org/example/cp/oms/domain/ability/extension/DefaultAssignOrderNoExt.java)
5557
- [中台统一定义,兼顾前台个性化的错误码机制](order-center-cp/cp-oc-spec/src/main/java/org/example/cp/oms/spec/exception/OrderException.java)
5658
- [中台特色的领域模型](order-center-cp/cp-oc-spec/src/main/java/org/example/cp/oms/spec/model/IOrderMain.java)
5759
- spec jar里定义受限的领域模型输出给业务前台:通过接口,而不是类
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
package org.example.cp.oms.domain.ability.extension;
22

33
import io.github.dddplus.annotation.Extension;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.example.cp.oms.domain.specification.ProductNotEmptySpec;
46
import org.example.cp.oms.spec.ext.IAssignOrderNoExt;
57
import org.example.cp.oms.spec.model.IOrderMain;
68

9+
import javax.annotation.Resource;
710
import javax.validation.constraints.NotNull;
811

912
@Extension(code = IAssignOrderNoExt.DefaultCode, value = "defaultAssignOrderNoExt")
13+
@Slf4j
1014
public class DefaultAssignOrderNoExt implements IAssignOrderNoExt {
1115

16+
@Resource
17+
private ProductNotEmptySpec productNotEmptySpec;
18+
1219
@Override
1320
public void assignOrderNo(@NotNull IOrderMain model) {
21+
// 演示调用业务约束的使用:把implicit business rules变成explicit
22+
if (!productNotEmptySpec.isSatisfiedBy(model)) {
23+
log.warn("Spec:{} not satisfied", productNotEmptySpec);
24+
//throw new OrderException(OrderErrorReason.SubmitOrder.ProductEmpty);
25+
}
1426

1527
}
1628
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.example.cp.oms.domain.specification;
2+
3+
import io.github.dddplus.annotation.Specification;
4+
import io.github.dddplus.specification.ISpecification;
5+
import io.github.dddplus.specification.Notification;
6+
import org.example.cp.oms.spec.model.IOrderMain;
7+
8+
// 之前的implicit business rules,现在变成了explicit rules
9+
@Specification("产品项不能空")
10+
public class ProductNotEmptySpec implements ISpecification<IOrderMain> {
11+
12+
@Override
13+
public boolean isSatisfiedBy(IOrderMain candidate, Notification notification) {
14+
if (candidate.productDelegate().getProducts() == null || candidate.productDelegate().getProducts().isEmpty()) {
15+
return false;
16+
}
17+
18+
return true;
19+
}
20+
}

order-center-cp/cp-oc-spec/src/main/java/org/example/cp/oms/spec/exception/OrderErrorReason.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public interface OrderErrorReason {
55
enum SubmitOrder implements OrderErrorSpec {
66
OrderConcurrentNotAllowed("101", "同一个订单不允许并发"),
77
InvalidExtenalNo("102", "非法的外部单号"),
8+
ProductEmpty("103", "产品为空"),
89
;
910

1011
private final String code;

0 commit comments

Comments
 (0)
0