Get smart with MapStruct Filip Hrisafov @filiphr Filip Hrisafov - - PowerPoint PPT Presentation

get smart with mapstruct
SMART_READER_LITE
LIVE PREVIEW

Get smart with MapStruct Filip Hrisafov @filiphr Filip Hrisafov - - PowerPoint PPT Presentation

Get smart with MapStruct Filip Hrisafov @filiphr Filip Hrisafov Project lead of MapStruct Platform architect at Flowable Open source engines (BPMN, CMMN, DMN) Contributor to various other open source projects


slide-1
SLIDE 1

Get smart with MapStruct

Filip Hrisafov @filiphr

slide-2
SLIDE 2

Filip Hrisafov

  • Project lead of MapStruct
  • Platform architect at Flowable

○ Open source engines (BPMN, CMMN, DMN)

  • Contributor to various other open source projects

filip.hrisafov@gmail.com @filiphr

JUG Zürich 2019 #MapStruct @filiphr

slide-3
SLIDE 3

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

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

Performance

https://github.com/arey/java-object-mapper-benchmark

JUG Zürich 2019 #MapStruct @filiphr

slide-7
SLIDE 7

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

slide-8
SLIDE 8

Demo

slide-9
SLIDE 9

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

slide-10
SLIDE 10

What else is there

  • Collection mappings
  • Update methods
  • Custom naming strategies
  • Configuration inheritance

○ Inverse methods ○ @MapperConfig

JUG Zürich 2019 #MapStruct @filiphr

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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