gpt4 book ai didi

java - 条件映射 ((条件) ? get-this : get-that) with Org. ModelMapper

转载 作者:行者123 更新时间:2023-11-30 02:04:35 24 4
gpt4 key购买 nike

我正在尝试根据 map 的根/源的条件有条件地使用一个或另一个 setter/getter 。 A 引用了 B;但是,它(引用)允许为空。当引用不为空时,我想使用它的属性(B)。但是,当引用为 null 时,我想使用源中的属性 (A)。

这有点业务逻辑,但我们的系统中有很多数据模型都遵循这种模式。使用映射器库是有益的,在我看来,逻辑非常简单 - 它与使用库的条件非常相似。

当我在 getter 中使用逻辑时,它将正常启动并初始化;然而,当映射实际用于映射对象时,我从 Model-Mapper 收到一个 IllegalArgumentException ,它表示 object 不是声明类的实例

我无法将库的条件正确地融入到解决方案中。它似乎更像是一劳永逸,而不是“如果或否则”。我有一个映射器,它首先使用源上的 getter (A)。然后,在下一行中,我调用条件 Conditions.isNotNull()) 并映射 a -> a.getB().getDescription()。因此,在我看来,它的工作原理是首先使用 A 设置 DTO 的 description 属性。然后,它会使用 a.getB().getDescription()“覆盖”该值,但前提是 a.getB() 不为 null。然而,它似乎并非如此。相反,对于 a.getB() 返回 null 的实例,我将 null 视为 DTO 的描述

总之,我希望能够执行以下操作:

modelMapper.createTypeMap(A.class, FlatAB.class).addMappings(mapper -> {
mapper.map(a -> a.getB() != null ? a.getB().getDescription() :
a.getDescription(), FlatAB::setDescription);
});

这里有一些示例代码来演示我面临的问题。

import java.util.Optional;
import org.junit.Assert;
import org.junit.Test;
import org.modelmapper.ModelMapper;

public class TestMappingConditionalGettersWithModelMapper {

@Test
public void testConditionalGetter() {
ModelMapper modelMapper = new ModelMapper();

modelMapper.createTypeMap(A.class, FlatAB.class).addMappings(mapper -> {

// mapper.map(a -> a.getB() != null ? a.getB().getDescription() : a.getDescription(), FlatAB::setDescription);

mapper.map(a -> Optional
.ofNullable(a.getB())
.map(B::getDescription)
.orElseGet(a::getDescription), FlatAB::setDescription);
});

// first try A with no relationship
A a = new A();
a.setDescription("description of A");
FlatAB flatAB1 = modelMapper.map(a, FlatAB.class);
Assert.assertEquals("they should equal", a.getDescription(), flatAB1.getDescription());


// now try it WITH a relationship
A a2 = new A();
a2.setDescription("description of A2");
B b = new B();
b.setDescription("description of B");
a2.setB(b);
FlatAB flatAB2 = modelMapper.map(a2, FlatAB.class);
Assert.assertEquals("they should equal", b.getDescription(), flatAB2.getDescription());
}

}

class A {

private String description;
private B b;

/*
.....
.....
many other properties
.....
....
*/

public B getB() {
return b;
}

public void setB(B b) {
this.b = b;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}

class B {

private String description;

/*
.....
.....
many other properties
.....
....
*/

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}

最佳答案

I popped the question over on the library's Github并收到了维护者的精彩答复,Chun-Han, Hsiao .

We can't map multiple source properties to a destination without a converter.

Please try:

modelMapper.addMappings(mapper ->
mapper.using(ctx -> ctx.getSource().getB() != null
? ctx.getSource().getB().getDescription()
: ctx.getSource().getDescription())
.map(src -> src, FlatAB::setDescription));

我发现ctx.getSource()可能需要强制转换——但我确信有一种方法可以提供该类型。

关于java - 条件映射 ((条件) ? get-this : get-that) with Org. ModelMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51726223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com