Cleaner way to add annotation functionality without using annotations
Brought to you by:
aruckerjones,
sconway
The library is easy to use with annotations, but these are an issue when building in hexagonal architecture and decoupling core objects from specific libraries.
I woud like to suggest a way to define annotations when creating the builder by method calls so that objects themselves can remain pure.
For example:
DateObject {
@CsvDate(value = "yyyy-MM-dd HH:mm:ss") //replace this with option below
Date date;
}
CsvToBeanBuilder<T>(inputStream)
.withType(clazz)
.withAnnotation(DateObject.class,"date","@CsvDate","yyyy-MM-dd HH:mm:ss")
.build()
I did check for a way to write this manually but have not found an elegant solution yet.
I subscribe to this. At the very least, if we are missing opencsv annotations, make it so that we can fallback to some other common annotations that we might already have such as javax persistence @Column or @JsonProperty...This would at least correctly map the fields for simple values.
Last edit: Razvan Dana 2023-02-28
TBH until this there has never been that many, if any, requests for annotation functionality without annoations.
Personally I am a big fan of decoupling and as such I would never use annotations on my core data classes. Instead I have always been a fan of what I call DTO (Data Transfer Objects) which are really closer to factory classes that are a representation of the csv we are getting that then produces the appropriate base class(es). That or a DTO class and a separate builder/factory object that will take the DTO and produce the base class. This way the base classes of my application are immune to changes of the libraries as well as potential changes to the csv file.
Agree with you Scott, its what I would like to achive - complete decouply of the core domain logic from the library so that, if needed, I can easily implement another adapter that would be providing the same features as the library already used.
Easy to test as well, as no changes to tests needed if interfaces/ports are being tested.
Just need an easy and humanly readable way to make opencsv parse date and other objects without any need of annotations.