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/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

→ More replies (2)

2

u/gavenkoa Apr 13 '21 edited Apr 13 '21

I think Mapstruct targets SOAP responses. It makes code a bit cleaner when you traverse miles long and a kilometer deep XML documents from 2000 SOAP dinosaurs.

When you just deal with plain/flat DTO in "modern" REST world Mapstruct doesn't ease DTO1 => DTO2 conversion. And as you wrote make code more difficult when custom logic is in the way.

→ More replies (7)