一、业务背景
在DDD战术落地—聚合的编排一定要在应用层吗?(领域服务与领域对象的区别)文中指出,若领域层只是单纯划分聚合根,实现数据与行为的一致,会导致一些领域能力外泄到应用层,即调用者需要有一定的领域知识进行编排,其实并不符合DDD领域驱动设计的思路。故采用了领域服务组合一个领域中的多个聚合根实体来对外输出领域能力。
而在落地实践中,我们发现,简单的领域可以仅仅使用领域对象来提供能力,复杂的领域必须借助与领域服务来提供能力。且随着业务的逐渐复杂。会引发领域能力的不清晰。所以需要规范化领域能力的提供方式。所以需要在领域层上搭建一层节点层(Node层)。应用层实现对Node节点的编排,最终系统中复杂的链路会以一个单向无环图的方式呈现出来。
二、上下文数据传递
在进行能力抽象时,会有一些提供通用且基础能力的节点,在不同的链路中会被复用。那么这些通用能力的节点的上下文如何定义呢?
2.1 继承实现(标准化Context来进行数据传递)
在美团外卖广告平台化的探索与实践中,是这样描述的:
使用的是典型的多类继承的方式,公共的Node节点使用的是父Context上下文。
优点:参数存取指向性明确,使用时无需定义魔法值,节点与图解耦(实现节点在多链路复用
缺点:扩展不便;context会日渐膨胀,维护成本高;
2.2 Map实现
以流程编排开源框架liteflow为例:
优点:存取方便,不会导致context膨胀,节点与图解耦。
缺点:存取时需要用户定义“魔法值”。
2.3 接口多继承与组合
- 需要被复用的上下文应声明为接口,以便多继承
假设有两个节点需要被复用:
public interface ContextA {
int getA();
void setA(int a);
}
public class ContextAImpl implements ContextA {
@Getter
@Setter
private int a;
public ContextAImpl(int a) {
this.a = a;
}
}
public interface ContextB {
int getB();
void setB(int b);
}
public class ContextBImpl implements ContextB {
@Getter
@Setter
private int b;
public ContextBImpl(int b) {
this.b = b;
}
}
业务相关的上下文定义:
使用的是多继承,于是ContextC了ContextA、ContextB是子类,可以通过多态的方式传递参数。
public interface ContextC extends ContextA, ContextB {
}
实现:使用了lombok的@Delegate
实现了父接口定义的方法。
public class ContextCImpl implements ContextC {
@Delegate(types = ContextA.class)
private final ContextA a;
@Delegate(types = ContextB.class)
private final ContextB b;
/**
* 业务特有字段
*/
private String c;
public ContextCImpl(ContextA a, ContextB b) {
this.a = a;
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
}
定义节点:
public interface Node<T, R> {
R execute(T param);
}
public class ContextNodeC implements Node<ContextC, String> {
@Override
public String execute(ContextC param) {
int a = param.getA();
int b = param.getB();
return a + " : " + b;
}
}
测试使用:
public class Test {
public static void main(String[] args) {
ContextNodeC contextNodeC = new ContextNodeC();
ContextC contextC = new ContextCImpl(new ContextAImpl(1), new ContextBImpl(2));
String execute = contextNodeC.execute(contextC);
System.out.println(execute);
}
}
2.4 Map+SerializedLambda方式
思路:
使用SerializedLambda获取到方法引用的方法名
反射工具类Generics:获取到泛型对象的泛型类型
当传入的是Lambda表达式实现了SerializedLambda接口,可以通过反射的方式来获取到参数类型与name。这样可以无需进行魔法值的转换而获取到转化后的类型。
- 定义lambda接口:
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Function;
import lombok.SneakyThrows;
import sun.reflect.generics.parser.SignatureParser;
import sun.reflect.generics.tree.ClassTypeSignature;
import sun.reflect.generics.tree.MethodTypeSignature;
public interface NamedFunction<T, R> extends Function<T, R>, TypedName<R>, Serializable {
R apply(T t);
@SneakyThrows
@Override
default Class<R> type() {
//调用writeReplace()方法,返回一个SerializedLambda对象
SerializedLambda lambda = this.lambda();
SignatureParser parser = SignatureParser.make();
MethodTypeSignature methodSig = parser.parseMethodSig(lambda.getImplMethodSignature());
ClassTypeSignature signature = (ClassTypeSignature) methodSig.getReturnType();
return (Class<R>) Class.forName(signature.getPath().get(0).getName());
}
@SneakyThrows
default String name() {
SerializedLambda lambda = lambda();
return lambda.getImplMethodName();
}
@SneakyThrows
default SerializedLambda lambda() {
Method method = this.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
//调用writeReplace()方法,返回一个SerializedLambda对象
return (SerializedLambda) method.invoke(this);
}
}
public interface TypedName<T> {
/**
* 对象类型
*/
default Class<T> type() {
return Generics.find(this.getClass(), TypedName.class, 0);
}
/**
* 对象名称
*/
String name();
}
- 定义Context上下文
public interface DefaultContext {
//定义
<T, R> Store<R> with(NamedFunction<T, R> name);
Map<String, Object> getContextMap();
@Slf4j
class Store<T> {
protected NamedFunction function;
protected DefaultContext data;
public Store(NamedFunction function, DefaultContext data) {
this.function = function;
this.data = data;
}
public T get() {
Object value = data.getContextMap().get(this.getKey());
if (value == null) {
return null;
} else if (value.getClass() != function.type()) {
log.error("key:{} value:{}, 存储类型{}与声明类型{}不一致!",
getKey(), value, value.getClass(), function.type());
}
return (T)value;
}
void set(Object value) {
data.getContextMap().put(getKey(), value);
}
public String getKey() {
return this.function.name();
}
}
}
实现类:
@Slf4j
public class LocalContext implements DefaultContext {
//定义存储结构
private Map<String, Object> contextMap = Maps.newConcurrentMap();
@Override
public <T, R> Store<R> with(NamedFunction<T, R> name) {
return new Store<>(name, this);
}
@Override
public Map<String, Object> getContextMap() {
return contextMap;
}
}
定义Context的存储类型:
/**
* 基于Context的存储结构
*/
public interface NameDef {
Long uid();
User user();
Person person();
}
测试方式:无需类型强转。
public class Test {
public static void main(String[] args) {
LocalContext localContext = new LocalContext();
localContext.with(NameDef::user).set(new User(1001L, "http://baidu.com", Lists.newArrayList()));
localContext.with(NameDef::uid).set(1001L);
Long uid = localContext.with(NameDef::uid).get();
User user = localContext.with(NameDef::user).get();
System.out.println(uid);
System.out.println(user);
}
}
3. 框架中如何从Context(Map实现)中映射子节点需要的参数
例如:定义了Node节点的类型。在订单链路中上下文Context为DefaultContext。需要调用基础Node(上下文为BaseContext)节点。如何从DefaultContext中拿到BaseContext所需要的参数?
public interface Node<T, R> {
R execute(T param);
default Class<T> paramType() {
return Generics.find(this.getClass(), Node.class, 1);
}
}
最简单的方式:
- DefaultContext维护的结构:class:子context
- 找到基础Node中声明的泛型类型class
- 从OrderContext中通过class找到param传递给下一个节点。
但是BaseContext的上下文,需要OrderContext上下文进行组装才可以拿到,并不简单的维护好结构。于是我们可以这样做:
//转化下一个子节点需要的参数类型
@SneakyThrows
private static <T, R> T parseParam(Node<T, R> nodeAction, LocalContext data) {
//获取到 子节点声明类型
Class<P> type = nodeAction.paramType();
//如果nodeAction的入参是LocalContext,直接赋值
if (type == LocalContext.class) {
return (T) data;
}
//如果nodeAction的入参是ContextConstructor的子类,说明定义了convert方法
if (ContextConstructor.class.isAssignableFrom(type)) {
//调用convert方法
ContextConstructor r = (ContextConstructor) type.newInstance();
return (P) r.convert(data);
}
log.error("声明的入参没有继承ContextConstructor");
return null;
}
我们需要BaseNode定义的上下文需要继承于ContextConstructor接口:
public interface ContextConstructor<P> {
P convert(LocalContext data);
}
实现类:
@Data
public class TestReq implements ContextConstructor<TestReq> {
private People people;
@Override
public TestReq convert(LocalContext data) {
this.people = data.with(Name::people).get();
return this;
}
}