Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Get smart with MapStruct (JUG Zurich 2019)

Get smart with MapStruct (JUG Zurich 2019)

Who wants to write mapping code? Using reflection based solutions comes with a host of tricky problems like runtime errors, difficulty debugging and then - of course - it's not type safe.

MapStruct is a new way of mapping data that generates Java code at compilation time. It's easy to debug, has no run time dependencies, is type safe and doesn't use reflection.

Drop the reflection! Come along and learn how to solve typical mapping problems with MapStruct.

Filip Hrisafov

August 27, 2019
Tweet

More Decks by Filip Hrisafov

Other Decks in Programming

Transcript

  1. Filip Hrisafov • Project lead of MapStruct • Platform architect

    at Flowable ◦ Open source engines (BPMN, CMMN, DMN) • Contributor to various other open source projects [email protected] @filiphr JUG Zürich 2019 #MapStruct @filiphr
  2. The Problem • Need to map between different models ◦

    View Model vs DB Model ◦ 3rd party REST API vs DB Model • Types are similar, but not quite the same Car - make: String - model: Model - numberOfSeats: int - manufacturingDate: LocalDate - price: BigDecimal - category: Category CarDto - make: String - model: String - seatCount: int - manufacturingDate: String - price: double - category: String JUG Zürich 2019 #MapStruct @filiphr
  3. Problem Solutions • Manually ◦ Boring code ◦ Error prone

    (might miss a mapping for a new property) • Reflection-based libraries ◦ Lack of type safety ◦ Performance penalty ◦ Difficult debugging JUG Zürich 2019 #MapStruct @filiphr
  4. Introducing MapStruct • Mappings defined as interfaces • Mapping java

    code generated at compile time using annotation processor • No reflection • No runtime dependencies JUG Zürich 2019 #MapStruct @filiphr
  5. Installation • Installation instructions - http://mapstruct.org/documentation/installation/ • Maven example -

    https://github.com/mapstruct/mapstruct-examples/blob/master/mapstruct-fie ld-mapping • Gradle example - https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-on- gradle • IDE Support - http://mapstruct.org/documentation/ide-support ◦ Eclipse Plugin ◦ IntelliJ Plugin JUG Zürich 2019 #MapStruct @filiphr
  6. Advantages of code generation • Type-safe • Quick feedback loop

    • Easy to debug • Plain method calls -> very fast • No runtime dependencies • Works on CLI or in your IDE JUG Zürich 2019 #MapStruct @filiphr
  7. What else is there • Collection mappings • Update methods

    • Custom naming strategies • Configuration inheritance ◦ Inverse methods ◦ @MapperConfig JUG Zürich 2019 #MapStruct @filiphr
  8. Future work • Make use of constructor arguments when instantiating

    mapping targets • Annotation composition (meta annotations) • Mapping from Object into Map JUG Zürich 2019 #MapStruct @filiphr
  9. Get smart with MapStruct • Reference guide, API docs etc.

    - mapstruct.org/ • Source code, IDE plugins, examples - https://github.com/mapstruct/ • Demo from this presentation - https://github.com/filiphr/jug_switzerland_2019 • Gitter channel - https://gitter.im/mapstruct/mapstruct-users • Discussion group - https://groups.google.com/forum/#!forum/mapstruct-users • Latest news @GetMapStruct or #MapStruct JUG Zürich 2019 #MapStruct @filiphr