Injecting Dependency in Mapper

454 views
Skip to first unread message

Philipp Ludewig

unread,
Feb 17, 2021, 3:04:36 AM2/17/21
to mapstru...@googlegroups.com, Mona Fenzl
Aloha,  
 
I would like to ask if it is possible to use instantiated objects in the Mapper classes. I couldn't find something in the documentation about this and I wonder if this is not possible by design.  I have seen the @uses annotation and my question evolves around this, as is there something similar for injecting other dependencies (non-mappers).
My use case is to use feature toggles in the mappers "default" methods. In order to use feature toggles I would like to inject an instance of a class called FeatureManager which needs to be auto wired from Spring. (I am using togglz library for feature toggles). I have tried using the abstract class approach but the auto-wiring annotation caused an error and i couldn't make it work.

Is there some way to do this with MapStruct or am i on a wooden path?  
 
Here is a very simple example of the use case:
```  
@Getter
@Setter
public class Customer {
private Long id;
private String name;
}

public class CustomerDto {
public Long id;
public String customerName;
}

@Mapper
public interface CustomerMapper {
CustomerMapper MAPPER = Mappers.getMapper( CustomerMapper.class );

@Mapping(source = "customerName", target = "name")
Customer toCustomer(CustomerDto customerDto);

default String customerNameToName(String name){
   if(featureManager.isActive(...)){ // How to inject this featureManager to the mapper?
   then do something....
   } else {
   do something else
   }
}
```

Thanks for help.

Stanislav Spiridonov

unread,
Feb 17, 2021, 5:20:27 AM2/17/21
to Philipp Ludewig, mapstru...@googlegroups.com, Mona Fenzl
Hi,
I am using the spring and the following snippet is working 

@Mapper(componentModel = "spring")
public abstract class MyMapper {

    @Value("${site}")
    private Site site;
    @Autowired
    private AvailableService availableService;

    @Mapping(target = "id", source = "id", qualifiedByName = "id")
    // After
    @Mapping(target = "clockStopped", ignore = true)
    public abstract Item map(Event event);

}

Stas.

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mapstruct-users/CAOJfx7B3GpAoq7uJq5FUicD-0TOtruwGv-s%2B-srWP9QmnNLgdA%40mail.gmail.com.

Philipp Ludewig

unread,
Feb 17, 2021, 12:20:35 PM2/17/21
to mapstruct-users
Aloha,
thanks for the answer. I feel i was to close to see this solution :D 
How would you test this mapper in a UnitTest and supply the dependency of "availableService" or do you test your mappers only in IntegrationTests?

Cheers,
Ludewig

Stanislav Spiridonov

unread,
Feb 17, 2021, 1:47:19 PM2/17/21
to mapstruct-users
Hi,

1. Simple test - new MyMapperImpl() + mappers custom method (inside  MyMapper) check if availableService is not null
or
2. Use mockito and inject the AvailableService as mock  (@MockInject +@Mock) 
or
3. Integration test with spring context. 

Of course, the variants above are the base, you can combine them as necessary.

Stas.
Reply all
Reply to author
Forward
0 new messages