简体   繁体   English

Orika 在运行时可选的转换/映射

[英]Orika optional conversion/mapping in runtime

I'm using Orika mapper to avoid boilerplate converters and have an interesting problem.我正在使用 Orika 映射器来避免样板转换器,并且有一个有趣的问题。 To simplify development, I need to create an Annotation that will determine when to map the value and when to leave it as is (not null or default, just as is).为了简化开发,我需要创建一个 Annotation 来确定何时映射值以及何时保持原样(不是 null 或默认值,原样)。

Imagine that we have 2 classes:想象一下,我们有 2 个类:

class Dto {
    public int id;
    public String name;
}

class Entity {
    public int id;
    public String name;
}

Dto comes from Front-end and we want to map the field name to an entity only if it starts with something like: Dto 来自 Front-end,我们希望将字段名称映射到实体,仅当它以以下内容开头时:

if {dto.name.startsWith("A")}
    entity.name = dto.name;

And we need similar logic in a lot of classes - only starting string is changed.而且我们在很多类中都需要类似的逻辑——只更改了起始字符串。

So I want to create annotation @IfStartsWith(String startsWith) and do following:所以我想创建注释@IfStartsWith(String startsWith)并执行以下操作:

class Entity{
    public int id;
    @IfStartsWith("A")
    public String name;
}

On project startup I want to config Orika mapper reading this annotation from needed classes and setup dynamically mapping behavior depending on this.在项目启动时,我想配置 Orika 映射器从所需的类中读取此注释,并根据此设置动态映射行为。

The important thing here is that I can't use default (null or empty string) because I need to leave destination value as is if check fails.这里重要的是我不能使用默认值(空或空字符串),因为如果检查失败,我需要保持目标值不变。 "As is" means either value that is in existing destination object or class default value (set as public String name = "My name"; ) “按原样”表示现有目标对象中的值或类默认值(设置为public String name = "My name";

I know how to get annotations from fields and it works now but the questions are:我知道如何从字段中获取注释并且它现在可以工作,但问题是:

What should I use : field-level converter or class-level customization?我应该使用什么:字段级转换器或类级自定义?

Is there any way to avoid reflection in field value setting inside these customized converters/mappers?有什么方法可以避免在这些自定义转换器/映射器中的字段值设置中发生反射

Ok, so I've managed to solve this (not saying it's pretty).好的,所以我设法解决了这个问题(不是说它很漂亮)。

Basically, I found that Orika can't "leave" field as is.基本上,我发现 Orika 不能按原样“离开”字段。 If you actually do a mapping from field to field then the value will be set whatever it is.如果您实际上进行了从字段到字段的映射,那么该值将被设置为任何值。

I read all fields using reflection and do following logic:我使用反射读取所有字段并执行以下逻辑:

if field has no @IfStartsWith annotation 
then it's mapped as usual:
   classMapBuilder.fieldAToB(*).fieldBToA(*)
if field has custom annotation 
then I create a CustomMapper object that handles the logic.

Please note that this is a class-level custom mapper.请注意,这是一个类级别的自定义映射器。

Important thing here is that you DON'T do standard mapping (fieldAToB, default and others).这里重要的是你不要做标准映射(fieldAToB、default 和其他)。 Before custom mapper Orika doesn't even know that there is such a field and it has to be mapped.在自定义映射器 Orika 甚至不知道有这样一个字段之前,它必须被映射。 Custom mapper uses reflection (because I don't know in advance which field will be mapped this way).自定义映射器使用反射(因为我事先不知道哪个字段会以这种方式映射)。

Hope this will help somebody.希望这会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM