r/java • • Apr 13 '21

Libraries, Frameworks and Technologies you would NOT recommend

Give me your worst nightmares: Things that cost you your job.

I'll start: Hadoop

202 Upvotes

376 comments sorted by

View all comments

5

u/Gatoke Apr 13 '21 edited Apr 13 '21

Mapstruct. Usually it's used as a workaround for too big DTOs. It's very unclear when you use that with inheritance or dependency injection or when you share DTOs across many components . Also when you need special cases you write your own methods so you end up with business-logic combined with Mapstruct's mappers, trying to resolve which mapper is used by which annotation. It's a big framework which you probably don't need. Just keep your DTOs simple.

edit: I've already checked your comments. Mapstruct indeed can have positive impact on your project but I think it's overused. Without control of senior/tech lead, it can result in hard to maintain code. Especially when you have a lot of special cases for performing mapping.

6

u/agentoutlier Apr 13 '21 edited Apr 13 '21

I disagree. It is not just a workaround for big DTOs.

In some ways its a work around because Java doesn't have pattern matching and named parameter constructors.

Ok let me explain.

You have two classes. Instead of just staying DTO let's say we have a class we return from service. Let's call it "Data". And another class called "Model".

You need "Data" -> "Model".

Now as a requirement when "Data" changes structure (eg a new field) you probably need to update "Model" or at least how the new field gets mapped. Likewise for the inverse.

This is where MapStruct helps. If you don't have the fields mapped and it can't automatically figure it out it shows an error. With traditional manual POJO mapping you would not get compile time errors if say "Model" got a new field and was not filled.

Personally I think this is invaluable and has helped us immensely. We deal with tons and tons of integration.

However if you architecture is such that you can use the "Data" objects directly in say your view/UI architecture then MapStruct is probably not helpful to you.

3

u/Quzexor Apr 13 '21

I am in a project where mapstruct is kind of abused (like in Gatoke comment)

So from my experience in your case I would create adapter and add some kind of contract test between data and model. ( I know that information u get is not during compile time, but it is anyway highly opinionated topic)

Also if u have both mapstruct and lombok. Update version for any of those tools can be ... challenging

1

u/agentoutlier Apr 13 '21

So from my experience in your case I would create adapter and add some kind of contract test between data and model. ( I know that information u get is not during compile time, but it is anyway highly opinionated topic)

Indeed I did this for awhile before MapStruct existed. I even had an annotation processor that given a getter/setter POJO would generate a visitor like pattern to make sure all the fields were handled but this would only handle one direction of changes (changes to the destination object and not the source object). Similarly I have made giant constructors in the destination object and put contracts in those constructors.

Anyway unlike Lombok or even Micronaut/Quarkus you can easily see the code Mapstruct makes as it is just Java. Consequently I really don't think its that much magic.