0

Started working on java spring boot for a project requirement I want to understand what is the best way to use ModelMapper for my scenario.

I have a model InputMsg:

public class InputMsg {
    String EquipmentNumber;
    //getters setters
}

Further, I have two DTOs OutputMsg and ErrorDesc

public class OutputMsg {
    public ErrorDesc EquipmentNumber;
   //getters setters
}

public class ErrorDesc {
    String Value;
    //getters setters
}

My requirement is to use the incoming input message and finally return a result in the OutputMsg format which I can further take to do some other calculations.

eg. InputMsg -- "ABCD1234" OutputMsg : EquipmentNumber.value = "ABCD1234"

What I have used is something like this with ModelMapper is :

ModelMapper modelMapper = new ModelMapper();
PropertyMap <InputMsg, OutputMsg> orderMap = 
        new PropertyMap <InputMsg,OutputMsg>() {
              protected void configure() {
                OutputMsg.getEquipmentNumber()
                    .setValue(InputMsg.getEquipmentNumber());
                }
    modelMapper.addMappings(orderMap);
    return OutputMsg;
}};

The Problem I see is if I have 100's of property i will have to write a 100 lines of code to map it.

How can i do it in a better way to automatically map the InputMsg value to Output.Msg

Help is appreciated Regards

pirho
  • 11,565
  • 12
  • 43
  • 70

1 Answers1

0

First of all: you should follow Java conventions in your field naming make fields private (there are getters and setters), so name your fields starting like:

private String equipmentNumber;
private ErrorDesc equipmentNumber;
private value;

However this is not so important but my example uses such naming. The following thing is more important.

The most powerful feature (at leas IMO) is that ModelMapper can associate data when some suitable convention is used in field naming without any configuration. For example if you had your InputMsg like:

@Getter @Setter
public InputMsg {
    private String equipmentNumberValue;
}

the mapping would be straightforward and work without any configuration. So with a proper design you can reduce a bunch of boilerplate code.

Now (IMO) people usually use terms mapper & adapter interchangeably. But they are not exactly the same thing, see for example this question. Mapper can use adapter as you have configured in your ModelMapper configuration.

The thing is that no mapper can make complex/arbitrary adaptations from class A to class B without explicitly telling it how to adapt and it requires an adapter. There is no workaround other than make an adapter that ModelMapper uses and if it needs to handle 100 field it has to. Depending on the case it might be able to be done in some generic way but it could make the code harder to understand.

Also if you need to code lots of adapter code for ModelMapper to use maybe the whole thing would be more clear if it was done without ModelMapper.

So if possible try to change your design so that ModelMapper can associate it by itself instead of coding lots of adapting code.

But with a proper design and following conventions that mapper understands good mapper can adapt something like in my example.

pirho
  • 11,565
  • 12
  • 43
  • 70