Possible optimized record construction in order to use as DTO

Anatoly Kupriyanov kan.izh at gmail.com
Thu Jan 11 16:25:30 UTC 2024


Currently there are some libs to overcome the limitation like
https://github.com/Randgalt/record-builder . However, I still think it
looks more like a hack and loses required parameters to be named. It should
be part of a language, but there are too many ways to do it and there are
some limitations and compromises... which makes it quite difficult to
please everyone.

On Thu, 11 Jan 2024 at 13:54, Roel Meeuws <r.j.meeuws at gmail.com> wrote:

> Dear all,
>
> This is my first message to this list, so I hope I am not proposing
> something which has already been discussed before. But here goes.
>
> In several projects at different companies I have seen the use of big bean
> classes with lots of data inside and getters, setters, toString, equals,
> hashcode added. In some projects the boilerplate was mitigated with Lombok,
> but of course recent JDKs provide the notion of Records.
>
> Now records are great data carriers with much less boilerplate, however,
> there is a particular problem when constructing them for very big sets.
> Consider a dto representing a line in some EU legal obligation report (of
> which there are many) that may heve hundreds of fields. When constructing
> such an object there is no idea of what parameter is being filled in in the
> code that is constructing the object
>
> e.g.
>
> record IrritatinglyBigEUReportLine(
>   long processingId;
>   ZonedDateTime reportTime;
>   String firstName;
>   String lastName;
>   String leiCode;
>   String legalReportingId;
>   BigDecimal riskAmount;
>   BigDecimal mifidRiskAmount;
>   BigDecimal mifid2FinancialRiskAmount;
>   BigDecimal mifid2SomeOtherAmount;
>   ......
> ) {}
>
> now let's construct this:
> var line = new IrritatinglyBigEUReportLine(
>   12345,
>   ZonedDateTime.now(),
>   "John",
>   "Doe",
>   "529900T8BM49AURSDO55",
>   BigDecimal.valueOf("100.0"),
>   BigDecimal.valueOf("100.0"),
>   BigDecimal.valueOf("100.0"),
>   BigDecimal.valueOf("100.0"), // anyone knows what this value is here?
>   ...
> );
>
> // also this will give a compile error in my IDE, which parameter is
> missing now?
>
> Could we introduce a better way of initialization like the following,
> which is like the `with`-er syntax, I have read Brian Goetz writing about.
> var line = new IrritatingLuBigEUReportLine with {
>   processingId =12345;
>   reportTime = ZonedDateTime.now();
>   firstName = "John";
>   lastName = "Doe";
>   leiCode = "529900T8BM49AURSDO55";
>   legalReportingId = "ERE43434452ERERTTT";
>   riskAmount = BigDecimal.valueOf("100.0");
>   mifidRiskAmount = BigDecimal.valueOf("100.0");
>   mifid2FinancialRiskAmount = BigDecimal.valueOf("100.0");
>   mifid2SomeOtherAmount = BigDecimal.valueOf("100.0");
>   ...
> };
>
> Roel
>
> --------------------------------------------
> Roel Meeuws
> Email: r.j.meeuws at gmail.com
> Mob. phone: +31 (0)6 10 82 44 01
> --------------------------------------------
>
>
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> Virus-free.www.avg.com
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#m_7710971634432452811_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>


-- 
WBR, Anatoly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240111/8d10dec4/attachment.htm>


More information about the amber-dev mailing list