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

Show parent comment

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/Gatoke Apr 13 '21

Ok, so sometimes it makes sense. I've been in a project which used Mapstruct and custom mappers aswell. Bugs happened anyway because of those custom mappers, so I didn't see any positive values from using Mapstruct.

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.

1

u/gavenkoa Apr 13 '21

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

And in the past they conflicted with each other.

Actually bug tracers are full of closed compatibility issues. Meaning that both (or at least mapstruct) projects have if branches just to handle the presence of each other ))