简体   繁体   English

在具有相同属性名称的不同数据类型上使用 ModelMapper

[英]Using ModelMapper on different data types with same attribute name

I have two classes say Animal & AnimalDto我有两个类说AnimalAnimalDto
I want to use ModelMapper to convert Entity to DTO and vice verca.我想使用ModelMapper将实体转换为 DTO,反之亦然。
But the classes are supposed to have different data types for a few attributes having similar name.但是对于一些具有相似名称的属性,这些类应该具有不同的数据类型。
How do I achieve this?我如何实现这一目标?

Animal.java动物.java

public class Animal {    
    int category;    
    String color;    
    int age;
}    
 

AnimalDto.java AnimalDto.java

public class AnimalDto {    
    String category;    
    int color;    
    int age;
}

currently I'm manually transforming as such:目前我正在手动转换:

class AnimalTransformer {
    private static Category[] categories = Category.values();    
    private static Color[] colors = Color.values();    

    animalEntityToDto(Animal animal) {
          AnimalDto animalDto = new AnimalDto();    
          animalDto.setAge(animal.getAge());
          animalDto.setCategory(categories[animal.getCategory()].toString());
          animalDto.setColor(Color.valueOf(animal.getColor()).ordinal());
    }

    animalDtoToEntity(AnimalDto animalDto) {
          Animal animal = new Animal();    
          animal.setAge(animalDto.getAge());
          animal.setCategory(Category.valueOf(animalDto.getCategory()).ordinal());
          animal.setColor(colors[animalDto.getColor()].toString());
    }
}

The example you presented might not be the best part of model mapper in fluency especially because converting enum s that have some special difficulties what comes to using generics.您提供的示例可能不是模型映射器流畅性的最佳部分,特别是因为转换enum具有一些特殊困难的使用泛型。

Anyway this is possible with Converter or usually AbstractConverter .无论如何,这可以使用Converter或通常是AbstractConverter

You did not provide examples of your enums so I create most simple example enums:您没有提供枚举示例,因此我创建了最简单的示例枚举:

enum Color {
    PINK;
}

and

enum Category {
    MAMMAL;
}

To convert Integer Animal.category to String AnimalDto.category , a converter could be like:要将Integer Animal.category转换为String AnimalDto.category ,转换器可能是这样的:

public class CategoryToStringConverter extends AbstractConverter<Integer, String> {
    @Override
    protected String convert(Integer source) {
        return Category.values()[source].toString();
    }
}

And to convert String Animal.color to Integer AnimalDto.category , a converter could be like:并将String Animal.color转换为Integer AnimalDto.category ,转换器可能如下所示:

public class ColorToOrdinalConverter extends AbstractConverter<String, Integer> {
    @Override
    protected Integer convert(String source) {
        return Color.valueOf(source).ordinal();
    }
}

Usage would be like:用法如下:

mm.createTypeMap(Animal.class, AnimalDto.class).addMappings(mapper -> {
    mapper.using(new CategoryToStringConverter()).map(Animal::getCategory, 
        AnimalDto::setCategory);
    mapper.using(new ColorToOrdinalConverter()).map(Animal::getColor, 
        AnimalDto::setColor);
});

This is the part of converting from Animal to AnimalDto .这是从Animal转换为AnimalDto Converting vice versa of course needs mappings of its own which I do not present here because I think the point came clear.反之亦然转换当然需要它自己的映射,我没有在这里介绍,因为我认为这一点很清楚。

For one class the way you do it now might be better but if you need to convert Category & Color in many places like this then you should consider using converters that are reusable.对于一个类,您现在这样做的方式可能会更好,但是如果您需要在许多地方像这样转换CategoryColor ,那么您应该考虑使用可重用的转换器。

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

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