record-builder
MapStruct
record-builder | MapStruct | |
---|---|---|
35 | 27 | |
762 | 7,167 | |
- | 0.8% | |
6.3 | 8.6 | |
10 days ago | 9 days ago | |
Java | Java | |
Apache License 2.0 | Apache License 2.0 |
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.
record-builder
-
JEP Draft – Derived Record Creation (Preview) – Java
The problem with this approach is that the people will have to build crutches like https://github.com/Randgalt/record-builder and by the time Java adds missing features all the codebases will be forever polluted with legacy workarounds that nobody will dare to remove because of backwards compatibility.
It also hinders adoption of new features as people will prefer to maintain consistency in their codebases.
-
Any library you would like to recommend to others as it helps you a lot? For me, mapstruct is one of them. Hopefully I would hear some other nice libraries I never try.
Record builder is pretty good for making builders for your Java 17 records
-
Useful & Unknown Java Libraries - Piotr's TechBlog
I always have to bring up RecordBuilder simply because of the included Withers and it being a source generator instead of the Lombok weirdness.
-
How to use Java Records
The above is not particularly user-friendly. Luckily compiler plugins can provide the missing feature, most notably RecordBuilder:
-
Named Parameters in Java
For records, instead of lombok you can use https://github.com/Randgalt/record-builder which is a valid annotation processor and will not break on JDK changes
-
has anyone written custom annotations using Lombok ?
In this particular case you can generate the builder with something like https://github.com/Randgalt/record-builder that is both valid java and lombok-like enough
-
I made a java client for the todoist api
Records + Record Builder or immutables + the trick to hide the implementing class w/sealed are your friend. Both the mutability and naming conventions this generates are vomitus.
-
"With" for records -- Brian Goetz
Did you use https://github.com/Randgalt/record-builder ?
-
What's your top Java pet peeve?
Try my annotation processor. It generates withers. https://github.com/Randgalt/record-builder
-
Stay with Java(+Spring) or pivot towards Go/Python?
I personally don't use that one much, but you could use this library instead.
MapStruct
-
What can happen if you skip the DTOs
You do not have to write the boilerplate code mapping the fields between your DTO and the other layers of your application. MapStruct can do it for you.
-
A single ChatGPT mistake cost us $10k
> You don't need to make DTOs when you don't have to, using AutoMapper is considered a bad practice and is heavily discouraged (if you do have to use a tool like that, there are alternatives like Mapperly which are zero-cost to use and will give you built-time information on what doesn't map without having to run the application).
The thing is, that you'll probably have entities mapped against the database schema with data that must only conditionally be shown to the users. For example, when an admin user requests OrderDetails then you'll most likely want to show all of the fields, but when an external user makes that request, you'll only want to show some of the fields (and not leak that those fields even exist).
DTOs have always felt like the right way to do that, however this also means that for every distinct type of user you might have more than one object per DB table. Furthermore, if you generate the EF entity mappings from the schema (say, if you handle migrations with a separate tool that has SQL scripts in it), then you won't make separate entities for the same table either. Ergo, it must be handled downstream somewhere.
Plus, sometimes you can't return the EF entities for serialization into JSON anyways, since you'd need to introduce some additional parsing logic, to get them into a shape that the front end wants (e.g. if you have a status field or something, the current value of which is calculated based on 5-10 database fields or other stuff).
Not to say that some of those can't be worked around, but I can't easily handwave those use cases away either. In Java, MapStruct works and does so pretty well: https://mapstruct.org/ I'd rather do something like that, than ask ChatGPT to transpose stuff from DDL or whatever, or waste time manually doing that.
-
Which tricks do you use to write less code?
I found some "tricks" to write less code, hence less code to maintain if there are any changes. Also less code with bugs just by changing the inputs.
For example, OpenAPI spec file + OpenAPI generator (https://github.com/OpenAPITools/openapi-generator). Any changes in the OpenAPI spec are reflected in the final code with a build step.
Another example: MapStruct (https://mapstruct.org/) to avoid passing data from Entity classes to DTO and back. Saves looots of boilerplate code.
Which are your tricks?
- Is .NET just miles ahead or am I delusional?
-
Object mapping libraries
In Java (woof..) we have MapStruct (https://mapstruct.org/). Anything like that in Python? I think maybe the Sqlalchemy mapping in Litestar2 is the closest I've seen.
-
Feedback on a new annotation processor api
Done right i can look something like mapstruct for example. But like any other feature you need to get a feeling for when it's a good idea to use it.
-
Mapping in Domain Driven Design sucks
We are using mapstruct but it sucks when you have protobuf :/
-
must known frameworks/libs/tech, every senior java developer must know(?)
You all beat me to MapStruct and Testcontainers. Honorable mention to RxJava, which I use in Desktop apps.
-
Correct way of exception handling, an optional?
For the mapping, I'm using Mapstruct, I got the instance example from their examples they use INSTANCE as constant.
- Ask HN: What are your “scratch own itch” projects?
What are some alternatives?
Lombok - Very spicy additions to the Java programming language.
ModelMapper - Intelligent object mapping
awesome-annotation-processing - A curated list of resources related to the Java annotation processing API (JSR 269)
JMapper Framework - Elegance, high performance and robustness all in one java bean mapper
core - An advanced and highly optimized Java library to build frameworks: it's useful for scanning class paths, generating classes at runtime, facilitating the use of reflection, scanning the filesystem, executing stringified source code and much more...
Orika - Simpler, better and faster Java bean mapping framework
Immutables - Annotation processor to create immutable objects and builders. Feels like Guava's immutable collections but for regular value objects. JSON, Jackson, Gson, JAX-RS integrations included
Dozer - Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another.
Jackson JSON Processor - Main Portal page for the Jackson project
Selma - Selma Java bean mapping that compiles
javageci - Java Code Generation Framework