title: Mapping a hardcoded value with ModelMapper
body: <p>I'm evaluating the <a href="http://modelmapper.org/" rel="nofollow noreferrer">ModelMapper library</a> to map a project DTOs and Entities. Although the library is quite powerful, I could not find a way to map a hardcoded value from on Entity to its DTO representation.</p>
  
  <p>The ModelMapper, maps a method from source class to a method on destination class. For example:</p>
  
  <pre><code>modelMapper.createTypeMap(MyEntity.class, MyDTO.class)
      .addMappings(mapper -&gt; mapper.map(MyEntity::getName, MyDTO::setFirstName))
      .addMappings(mapper -&gt; mapper.map(MyEntity::getSurname, MyDTO::setLastName))
  </code></pre>
  
  <p>But I've an attribute on DTO which is not present on the entity side yet. Using a Spring converter class, this mapping was always done by a hardcoded value like this <code>dto.setStatus("ACTIVE");</code>. But using ModelMapper I can't figure out how to do it properly. My first attempt was something like that:</p>
  
  <pre><code>    .addMappings(mapper -&gt; mapper.map(s -&gt; {return "ACTIVE";}, MyDTO::setStatus))
  </code></pre>
  
  <p>However, the mapping above does not work since ModelMapper expects to map a <code>get</code> method from the source. Thus, a return has no effect and in fact the code above results in runtime error.</p>
  
  <p>A possible solution, which is quite ugly is to force the desired result with <code>using</code> helper like this:</p>
  
  <pre><code>addMappings(mapper -&gt; mapper.using(c -&gt; "ACTIVE").map(MyEntity::getName, MyDTO::setStatus)); 
  </code></pre>
  
  <p>In this case, the <code>getName</code> method is used just to provide a <code>get</code> method while the real value will be replaced by <code>ACTIVE</code>. But since this is a unclear solution, I would like to understand whether there is a better solution to solve this <strong>using ModelMapper</strong>. </p>
  
  <p>Another solution is add to my entity/dto a method <code>public String getStatus() { return "ACTIVE"; }</code>. But I really would like to keep all hardcoded values on mappers.</p>