From 0a49960ee42276d772d2cfef54ab611a71025b74 Mon Sep 17 00:00:00 2001 From: chentaoah <609580885@qq.com> Date: Sat, 28 Dec 2024 22:08:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E5=8E=82=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E9=80=82=E5=BA=94=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/api/factory/EntityAdapter.java | 24 ++++++++ .../impl/factory/AdaptiveEntityAdapter.java | 59 +++++++++++++++++++ .../impl/factory/DefaultEntityFactory.java | 12 +++- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 dorive-core/src/main/java/com/gitee/dorive/core/api/factory/EntityAdapter.java create mode 100644 dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/AdaptiveEntityAdapter.java diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/api/factory/EntityAdapter.java b/dorive-core/src/main/java/com/gitee/dorive/core/api/factory/EntityAdapter.java new file mode 100644 index 00000000..4fe8c45e --- /dev/null +++ b/dorive-core/src/main/java/com/gitee/dorive/core/api/factory/EntityAdapter.java @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.dorive.core.api.factory; + +public interface EntityAdapter { + + Class adaptEntityType(Object persistent); + +} diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/AdaptiveEntityAdapter.java b/dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/AdaptiveEntityAdapter.java new file mode 100644 index 00000000..e89fcf6a --- /dev/null +++ b/dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/AdaptiveEntityAdapter.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.gitee.dorive.core.impl.factory; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.lang.Assert; +import cn.hutool.core.lang.func.Func1; +import cn.hutool.core.lang.func.LambdaUtil; +import com.gitee.dorive.api.entity.core.EntityElement; +import com.gitee.dorive.core.api.factory.EntityAdapter; +import lombok.Getter; +import lombok.Setter; + +import java.util.Map; + +@Getter +@Setter +public class AdaptiveEntityAdapter implements EntityAdapter { + + private EntityElement entityElement; + private String field; + private String alias; + private Map> valueEntityTypeMap; + + public AdaptiveEntityAdapter(EntityElement entityElement, Func1 func, Map> valueEntityTypeMap) { + Assert.notNull(entityElement, "The entityElement cannot be null!"); + Assert.notNull(func, "The func cannot be null!"); + Assert.notEmpty(valueEntityTypeMap, "The valueEntityTypeMap cannot be empty!"); + this.entityElement = entityElement; + this.field = LambdaUtil.getFieldName(func); + this.alias = entityElement.toAlias(field); + this.valueEntityTypeMap = valueEntityTypeMap; + } + + @Override + public Class adaptEntityType(Object persistent) { + Object fieldValue = BeanUtil.getFieldValue(persistent, alias); + if (fieldValue == null) { + return entityElement.getGenericType(); + } + return valueEntityTypeMap.getOrDefault(fieldValue, entityElement.getGenericType()); + } + +} diff --git a/dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/DefaultEntityFactory.java b/dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/DefaultEntityFactory.java index 28a6fd4a..7a2b2295 100644 --- a/dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/DefaultEntityFactory.java +++ b/dorive-core/src/main/java/com/gitee/dorive/core/impl/factory/DefaultEntityFactory.java @@ -23,6 +23,7 @@ import com.gitee.dorive.api.entity.core.EntityElement; import com.gitee.dorive.api.entity.core.PropertyDefinition; import com.gitee.dorive.api.entity.core.def.PropertyDef; import com.gitee.dorive.core.api.context.Context; +import com.gitee.dorive.core.api.factory.EntityAdapter; import com.gitee.dorive.core.api.factory.EntityFactory; import com.gitee.dorive.core.api.factory.EntityMapper; import com.gitee.dorive.core.entity.common.BoundedContext; @@ -47,16 +48,21 @@ public class DefaultEntityFactory implements EntityFactory { private EntityElement entityElement; private EntityStoreInfo entityStoreInfo; + // 序列化 private EntityMapper entityMapper; private CopyOptions reCopyOptions; private CopyOptions deCopyOptions; + // 边界上下文 private String boundedContextName; private BoundedContext boundedContext; private CopyOptions ctxCopyOptions; + // 适配器 + private EntityAdapter entityAdapter; public void setEntityElement(EntityElement entityElement) { this.entityElement = entityElement; initCtxCopyOptions(); + initEntityAdapter(); } private void initCtxCopyOptions() { @@ -73,6 +79,10 @@ public class DefaultEntityFactory implements EntityFactory { } } + protected void initEntityAdapter() { + this.entityAdapter = (persistent) -> entityElement.getGenericType(); + } + public void setEntityMapper(EntityMapper entityMapper) { this.entityMapper = entityMapper; initReCopyOptions(); @@ -130,7 +140,7 @@ public class DefaultEntityFactory implements EntityFactory { } public Object reconstitute(Context context, Object persistent) { - return BeanUtil.toBean(persistent, entityElement.getGenericType(), reCopyOptions); + return BeanUtil.toBean(persistent, entityAdapter.adaptEntityType(persistent), reCopyOptions); } @Override -- Gitee