Search code examples
parametersinterfacemappingabstract-classmapstruct

MapStruct best practice - @Context parameter or field in interface/abstract class?


I wanted to know which is considered better practice when using MapStruct - passing in @Context parameters, or having clean source-to-target mapping functions and having any external parameters I need as fields in the interface/abstract class? Thanks!


Solution

  • It really depends what you need to do. If you use @Context you'll have to pass it to all the methods you need.

    If you have something which is dynamic, based on some request parameters then you'll need to use @Context.

    If you have something which is a singleton, like a service you want to use, then maybe injecting it in your mapper is the best way to go.