Orika 映射器 - 字段子类型
Orika mapper - Field subtype
我有以下具体的类:
public class A1 {}
public class B1 extends A1 {}
public class A2 {}
public class B2 extends A2 {}
public class B3 extends A2 {}
而且我想在映射时得到一个B2实例MapperFacade.map(b1Instance, A2.class)
我需要这个,因为 A2 有很多子类型(如图所示的 B2 和 B3),我需要在需要时映射合适的子类型
Orika 可以做到这一点吗?
对于将面临同样问题的人,我解决了这个问题,创建了一个像这样的 CustomMapper 来验证哪个是要实例化的子类型
mapperFactory.classMap(SOURCE.class, DEST.class)
.customize(new CustomMapper<SOURCE, DEST>() {
@Override
public void mapAtoB(SOURCE source, DEST dest, MappingContext context) {
//verifies the type to instantiate
private A2 a2 = isB2() ? new B2() : new B3;
//use the mapper for the class (will use the B2 or B3 mapper)
mapperFacade.map(source.getA2(), a2);
// set the field into the dest object
dest.setB1(a2);
}
}
我有以下具体的类:
public class A1 {}
public class B1 extends A1 {}
public class A2 {}
public class B2 extends A2 {}
public class B3 extends A2 {}
而且我想在映射时得到一个B2实例MapperFacade.map(b1Instance, A2.class)
我需要这个,因为 A2 有很多子类型(如图所示的 B2 和 B3),我需要在需要时映射合适的子类型
Orika 可以做到这一点吗?
对于将面临同样问题的人,我解决了这个问题,创建了一个像这样的 CustomMapper 来验证哪个是要实例化的子类型
mapperFactory.classMap(SOURCE.class, DEST.class)
.customize(new CustomMapper<SOURCE, DEST>() {
@Override
public void mapAtoB(SOURCE source, DEST dest, MappingContext context) {
//verifies the type to instantiate
private A2 a2 = isB2() ? new B2() : new B3;
//use the mapper for the class (will use the B2 or B3 mapper)
mapperFacade.map(source.getA2(), a2);
// set the field into the dest object
dest.setB1(a2);
}
}