DEV Community

Cover image for Design with Patterns: Elegant Data Mapping
Roman V
Roman V

Posted on • Originally published at github.com

Design with Patterns: Elegant Data Mapping

Copyright (c) 2025 Roman Vidayko
www.linkedin.com/in/roman-vidayko

๐—œ๐—ก๐—ง๐—ฅ๐—ข

I decided to share my thoughts on how to make a software developer's life simpler and more efficient. It is not my intention to provide all-in-one silver bullets, but rather to offer some mindful ideas with supporting code snippets that provide fundamental understanding.

๐—œ๐—ก๐—–๐—œ๐—ง๐—˜

Being developing complex software, you've probably encountered the routine of converting a data model from local data types to remote ones and vice versa. Have you ever wondered what ideal data mapping should look like?
Here, I am going to introduce a solution that uses the power of Java and is based on the fundamental principles of Design Patterns. But let's first take a look at how it is usually done to understand the pros and cons.

๐—–๐—ข๐— ๐— ๐—ข๐—ก ๐—ฃ๐—ฅ๐—”๐—–๐—ง๐—œ๐—–๐—˜

Here are a couple of samples sketched out that are present in any project, like the tentacles of the Lernaean Hydraโ€”no matter how many you cut off, new ones will still grow.

๐—ข๐—ฝ๐˜๐—ถ๐—ผ๐—ป ๐Ÿญ: ๐——๐—ถ๐—ฟ๐—ฒ๐—ฐ๐˜ ๐—ฐ๐—ผ๐—ป๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ถ๐—ผ๐—ป

๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท ๐š›๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท = ๐š—๐šŽ๐š  ๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท(
๐š•๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท.๐š๐š’๐šŽ๐š•๐š๐Ÿท(),
...
๐š•๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท.๐š๐š’๐šŽ๐š•๐š๐™ฝ());

PอŸrอŸoอŸsอŸ: ๐˜๐˜ต ๐˜ช๐˜ด ๐˜ด๐˜ช๐˜ฎ๐˜ฑ๐˜ญ๐˜ฆ, ๐˜ข๐˜ฏ๐˜ฅ ๐˜บ๐˜ฐ๐˜ถ ๐˜ฅ๐˜ฐ๐˜ฏโ€™๐˜ต ๐˜ณ๐˜ฆ๐˜ข๐˜ญ๐˜ญ๐˜บ ๐˜ฏ๐˜ฆ๐˜ฆ๐˜ฅ ๐˜ต๐˜ฐ ๐˜ฌ๐˜ฏ๐˜ฐ๐˜ธ ๐˜‘๐˜ข๐˜ท๐˜ข ๐˜ต๐˜ฐ ๐˜ช๐˜ฎ๐˜ฑ๐˜ญ๐˜ฆ๐˜ฎ๐˜ฆ๐˜ฏ๐˜ต ๐˜ต๐˜ฉ๐˜ช๐˜ด! ๐Ÿฅด
CอŸoอŸnอŸsอŸ: ๐˜›๐˜ฉ๐˜ฆ ๐˜ฎ๐˜ฆ๐˜ต๐˜ฉ๐˜ฐ๐˜ฅ ๐˜ฃ๐˜ฐ๐˜ฅ๐˜บ ๐˜ต๐˜ฉ๐˜ข๐˜ต ๐˜ค๐˜ฐ๐˜ฏ๐˜ต๐˜ข๐˜ช๐˜ฏ๐˜ด ๐˜ช๐˜ต ๐˜ช๐˜ด ๐˜ด๐˜ฑ๐˜ฐ๐˜ช๐˜ญ๐˜ฆ๐˜ฅ ๐˜ธ๐˜ช๐˜ต๐˜ฉ ๐˜ญ๐˜ฐ๐˜จ๐˜ช๐˜ค ๐˜ต๐˜ฉ๐˜ข๐˜ต ๐˜ช๐˜ด ๐˜ฐ๐˜ถ๐˜ต ๐˜ฐ๐˜ง ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฎ๐˜ฆ๐˜ต๐˜ฉ๐˜ฐ๐˜ฅ'๐˜ด ๐˜ณ๐˜ฆ๐˜ด๐˜ฑ๐˜ฐ๐˜ฏ๐˜ด๐˜ช๐˜ฃ๐˜ช๐˜ญ๐˜ช๐˜ต๐˜บ ๐˜ด๐˜ค๐˜ฐ๐˜ฑ๐˜ฆ.

๐—ข๐—ฝ๐˜๐—ถ๐—ผ๐—ป ๐Ÿฎ: ๐—จ๐˜€๐—ฎ๐—ด๐—ฒ ๐—ผ๐—ณ ๐— ๐—ฎ๐—ฝ๐—ฝ๐—ฒ๐—ฟ ๐—จ๐˜๐—ถ๐—น๐—ถ๐˜๐˜† ๐—–๐—น๐—ฎ๐˜€๐˜€

๐š๐š’๐š—๐šŠ๐š• ๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท ๐š›๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท = ๐š‚๐š๐šŠ๐š๐š’๐šŒ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›.๐š–๐šŠ๐š™๐šƒ๐š˜๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท(๐š•๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท);
...
๐šŒ๐š•๐šŠ๐šœ๐šœ ๐š‚๐š๐šŠ๐š๐š’๐šŒ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š› {

๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐šœ๐š๐šŠ๐š๐š’๐šŒ ๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท ๐š–๐šŠ๐š™๐šƒ๐š˜๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท(๐™ป๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท ๐š•๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท){
๐š›๐šŽ๐š๐šž๐š›๐š— ๐š—๐šŽ๐š  ๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿท(
...,
๐š–๐šŠ๐š™๐šƒ๐š˜๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿธ(๐š•๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท));
}

๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐šœ๐š๐šŠ๐š๐š’๐šŒ ๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿธ ๐š–๐šŠ๐š™๐šƒ๐š˜๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿธ(๐™ป๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท ๐š•๐š˜๐šŒ๐šŠ๐š•๐šƒ๐šข๐š™๐šŽ๐Ÿท){
๐š›๐šŽ๐š๐šž๐š›๐š— ๐š—๐šŽ๐š  ๐š๐šŽ๐š–๐š˜๐š๐šŽ๐šƒ๐šข๐š™๐šŽ๐Ÿธ(
...
}
}

PอŸrอŸoอŸsอŸ: ๐˜Ž๐˜ฐ๐˜ต ๐˜ณ๐˜ช๐˜ฅ ๐˜ฐ๐˜ง ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฎ๐˜ข๐˜ฑ๐˜ฑ๐˜ช๐˜ฏ๐˜จ ๐˜ญ๐˜ฐ๐˜จ๐˜ช๐˜ค ๐˜ง๐˜ช๐˜น๐˜ช๐˜ฏ๐˜จ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฎ๐˜ฆ๐˜ต๐˜ฉ๐˜ฐ๐˜ฅ'๐˜ด ๐˜ด๐˜ช๐˜ฏ๐˜จ๐˜ญ๐˜ฆ ๐˜ณ๐˜ฆ๐˜ด๐˜ฑ๐˜ฐ๐˜ฏ๐˜ด๐˜ช๐˜ฃ๐˜ช๐˜ญ๐˜ช๐˜ต๐˜บ.
CอŸoอŸnอŸsอŸ: ๐˜›๐˜ช๐˜จ๐˜ฉ๐˜ต ๐˜ค๐˜ฐ๐˜ถ๐˜ฑ๐˜ญ๐˜ช๐˜ฏ๐˜จ ๐˜ธ๐˜ช๐˜ต๐˜ฉ ๐˜ค๐˜ฐ๐˜ฏ๐˜ด๐˜ถ๐˜ฎ๐˜ฆ๐˜ณ๐˜ด, ๐˜ช๐˜ฏ๐˜ข๐˜ฃ๐˜ช๐˜ญ๐˜ช๐˜ต๐˜บ ๐˜ต๐˜ฐ ๐˜ค๐˜ถ๐˜ด๐˜ต๐˜ฐ๐˜ฎ๐˜ช๐˜ป๐˜ฆ ๐˜ฎ๐˜ฆ๐˜ต๐˜ฉ๐˜ฐ๐˜ฅ๐˜ด, ๐˜ฏ๐˜ข๐˜ฎ๐˜ฆ๐˜ด๐˜ฑ๐˜ข๐˜ค๐˜ฆ ๐˜ฑ๐˜ฐ๐˜ญ๐˜ญ๐˜ถ๐˜ต๐˜ช๐˜ฐ๐˜ฏ, ๐˜ญ๐˜ฐ๐˜ธ ๐˜ฎ๐˜ข๐˜ช๐˜ฏ๐˜ต๐˜ข๐˜ช๐˜ฏ๐˜ข๐˜ฃ๐˜ช๐˜ญ๐˜ช๐˜ต๐˜บ ๐˜ฐ๐˜ง ๐˜ถ๐˜ต๐˜ช๐˜ญ๐˜ช๐˜ต๐˜บ ๐˜ค๐˜ญ๐˜ข๐˜ด๐˜ด๐˜ฆ๐˜ด, ๐˜ข๐˜ฏ๐˜ฅ ๐˜ฑ๐˜ฐ๐˜ต๐˜ฆ๐˜ฏ๐˜ต๐˜ช๐˜ข๐˜ญ ๐˜ฎ๐˜ฆ๐˜ฎ๐˜ฐ๐˜ณ๐˜บ ๐˜ญ๐˜ฆ๐˜ข๐˜ฌ๐˜ด ๐˜ธ๐˜ฉ๐˜ฆ๐˜ฏ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฏ๐˜ถ๐˜ฎ๐˜ฃ๐˜ฆ๐˜ณ ๐˜ฐ๐˜ง ๐˜ด๐˜ต๐˜ข๐˜ต๐˜ช๐˜ค ๐˜ฎ๐˜ฆ๐˜ต๐˜ฉ๐˜ฐ๐˜ฅ๐˜ด ๐˜จ๐˜ณ๐˜ฐ๐˜ธ๐˜ด ๐˜ถ๐˜ฏ๐˜ค๐˜ฐ๐˜ฏ๐˜ต๐˜ณ๐˜ฐ๐˜ญ๐˜ญ๐˜ข๐˜ฃ๐˜ญ๐˜บ.

๐—ข ๐—•๐—ฅ๐—”๐—ฉ๐—˜ ๐—ก๐—˜๐—ช ๐—ฃ๐—”๐—œ๐—ก๐—Ÿ๐—˜๐—ฆ๐—ฆ ๐— ๐—”๐—ฃ๐—ฃ๐—œ๐—ก๐—š

Thinking about this, I was looking for a solution that has a strong, clear pattern with a minimal number of checkpoints, something simple to understand and implement. It should be so clear and simple that my peers will be inclined to follow it, rather than improvising with rudimentary hacks.
The basis of this solution involves two design patterns: ๐˜š๐˜ต๐˜ณ๐˜ข๐˜ต๐˜ฆ๐˜จ๐˜บ and ๐˜š๐˜ฆ๐˜ณ๐˜ท๐˜ช๐˜ค๐˜ฆ ๐˜“๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ฐ๐˜ณ. The ๐˜š๐˜ต๐˜ณ๐˜ข๐˜ต๐˜ฆ๐˜จ๐˜ช๐˜ฆ๐˜ด will perform the mapping, and the ๐˜š๐˜ฆ๐˜ณ๐˜ท๐˜ช๐˜ค๐˜ฆ ๐˜“๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ฐ๐˜ณ will be responsible for selecting the right mappers. Additionally, the ๐˜Š๐˜ฐ๐˜ฏ๐˜ต๐˜ฆ๐˜น๐˜ต ๐˜–๐˜ฃ๐˜ซ๐˜ฆ๐˜ค๐˜ต pattern is involved. This is because local and remote data sometimes only partially overlap, and the ๐˜Š๐˜ฐ๐˜ฏ๐˜ต๐˜ฆ๐˜น๐˜ต ๐˜–๐˜ฃ๐˜ซ๐˜ฆ๐˜ค๐˜ต is designed to bring together all the data that needs to be contained by a target data type.
Thatโ€™s a lot of words, letโ€™s jump into fฬถiฬถgฬถhฬถtฬถ coding!

๐—›๐—ข๐—ช ๐—ง๐—ข ๐—จ๐—ฆ๐—˜

// ๐™ผ๐šŠ๐š™๐š™๐š’๐š—๐š ๐š๐š˜ ๐š›๐šŽ๐š–๐š˜๐š๐šŽ ๐™ฐ๐™ฟ๐™ธ ๐š๐šŠ๐š๐šŠ๐š๐šข๐š™๐šŽ
๐š๐š’๐š—๐šŠ๐š• ๐šƒ๐šข๐š™๐šŽ๐Ÿท ๐š๐šข๐š™๐šŽ๐Ÿท = ๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐™ป๐š˜๐šŒ๐šŠ๐š๐š˜๐š›.๐š–๐šŠ๐š™(๐š—๐šŽ๐š  ๐šƒ๐šข๐š™๐šŽ๐Ÿท๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š(๐š๐šข๐š™๐šŽ๐Ÿท๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š๐™ณ๐šŠ๐š๐šŠ));

// ๐™ฒ๐šŠ๐š•๐š•๐š’๐š—๐š ๐š›๐šŽ๐š–๐š˜๐š๐šŽ ๐™ฐ๐™ฟ๐™ธ
๐š๐š’๐š—๐šŠ๐š• ๐šƒ๐šข๐š™๐šŽ๐Ÿธ ๐š๐šข๐š™๐šŽ๐Ÿธ = ๐š›๐šŽ๐š–๐š˜๐š๐šŽ๐™ฐ๐š™๐š’๐™ฐ๐š๐šŠ๐š™๐š๐šŽ๐š›.๐š๐šŽ๐š๐š๐šŽ๐š–๐š˜๐š๐šŽ๐™ณ๐šŠ๐š๐šŠ(๐š๐šข๐š™๐šŽ๐Ÿท);

// ๐™ผ๐šŠ๐š™๐š™๐š’๐š—๐š ๐š๐š˜ ๐š•๐š˜๐šŒ๐šŠ๐š• ๐š๐šŠ๐š๐šŠ๐š๐šข๐š™๐šŽ ๐š๐š›๐š˜๐š– ๐š›๐šŽ๐š–๐š˜๐š๐šŽ ๐™ฐ๐™ฟ๐™ธ ๐š๐šŠ๐š๐šŠ๐š๐šข๐š™๐šŽ
๐šƒ๐šข๐š™๐šŽ๐Ÿน ๐š๐šข๐š™๐šŽ๐Ÿน = ๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐™ป๐š˜๐šŒ๐šŠ๐š๐š˜๐š›.๐š–๐šŠ๐š™(๐š—๐šŽ๐š  ๐šƒ๐šข๐š™๐šŽ๐Ÿน๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š(๐š๐šข๐š™๐šŽ๐Ÿธ));

How do you like the way the new mapping looks? Easy-peasy? Just calling the mapperLocator's single method, and that's it!
Letโ€™s go under the hood and see if the technical side of the solution is just as wonderful?

๐—ฆ๐—œ๐—ฅ ๐— ๐—”๐—ฃ๐—ฃ๐—˜๐—ฅ-๐—Ÿ๐—ข๐—–๐—”๐—ง๐—ข๐—ฅ

๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐šŒ๐š•๐šŠ๐šœ๐šœ ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›๐™ป๐š˜๐šŒ๐šŠ๐š๐š˜๐š› {
๐š™๐š›๐š’๐šŸ๐šŠ๐š๐šŽ ๐š๐š’๐š—๐šŠ๐š• ๐™ผ๐šŠ๐š™<๐™ฒ๐š•๐šŠ๐šœ๐šœ<๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ>, ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<? ๐šŽ๐šก๐š๐šŽ๐š—๐š๐šœ ๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ, ?>> ๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐šœ = ๐š—๐šŽ๐š  ๐™ท๐šŠ๐šœ๐š‘๐™ผ๐šŠ๐š™<>();

๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›๐™ป๐š˜๐šŒ๐šŠ๐š๐š˜๐š›(๐™ป๐š’๐šœ๐š<๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<? ๐šŽ๐šก๐š๐šŽ๐š—๐š๐šœ ๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ, ?>> ๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐šœ) {
๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐šœ.๐šœ๐š๐š›๐šŽ๐šŠ๐š–().๐š๐š˜๐š›๐™ด๐šŠ๐šŒ๐š‘(๐š๐š‘๐š’๐šœ::๐š›๐šŽ๐š๐š’๐šœ๐š๐šŽ๐š›);
}

๐š™๐š›๐š’๐šŸ๐šŠ๐š๐šŽ ๐šŸ๐š˜๐š’๐š ๐š›๐šŽ๐š๐š’๐šœ๐š๐šŽ๐š›(๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<? ๐šŽ๐šก๐š๐šŽ๐š—๐š๐šœ ๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ, ?> ๐š–๐šŠ๐š™๐š™๐šŽ๐š›){
๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐šœ.๐š™๐šž๐š(๐š๐šŽ๐š๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ๐šƒ๐šข๐š™๐šŽ(๐š–๐šŠ๐š™๐š™๐šŽ๐š›), ๐š–๐šŠ๐š™๐š™๐šŽ๐š›);
}

๐š™๐šž๐š‹๐š•๐š’๐šŒ <๐šƒ> ๐šƒ ๐š–๐šŠ๐š™(๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ) {
๐š›๐šŽ๐š๐šž๐š›๐š— ((๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ, ๐šƒ>) ๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐šœ.๐š๐šŽ๐š(๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ.๐š๐šŽ๐š๐™ฒ๐š•๐šŠ๐šœ๐šœ())).๐š–๐šŠ๐š™(๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ);
}

๐š™๐š›๐š’๐šŸ๐šŠ๐š๐šŽ ๐™ฒ๐š•๐šŠ๐šœ๐šœ<๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ> ๐š๐šŽ๐š๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ๐šƒ๐šข๐š™๐šŽ(๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<? ๐šŽ๐šก๐š๐šŽ๐š—๐š๐šœ ๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ, ?> ๐š–๐šŠ๐š™๐š™๐šŽ๐š›) {
// ๐š‚๐š˜๐š–๐šŽ ๐™น๐šŠ๐šŸ๐šŠ ๐š๐šŽ๐š๐š•๐šŽ๐šŒ๐š๐š’๐š˜๐š— ๐šŒ๐š˜๐š๐šŽ ๐š ๐š‘๐šŠ๐š ๐š’๐š๐šŽ๐š—๐š๐š’๐š๐š’๐šŽ๐šœ ๐š๐š‘๐šŽ ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ ๐š๐šŠ๐š๐šŠ-๐š๐šข๐š™๐šŽ ๐š๐š˜๐š› ๐šŠ ๐š–๐šŠ๐š™๐š™๐šŽ๐š›
}
}

No magic, just an ordinary job. The source types are keys to identify the right mapper. Source is just a marker interface. Simply supply the mapper instances to the constructor, or declare them as beans if it's a Spring application.

๐—ง๐—›๐—˜ ๐— ๐—œ๐—ก๐—œ๐—ข๐—ก๐—ฆ

๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐š’๐š—๐š๐šŽ๐š›๐š๐šŠ๐šŒ๐šŽ ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ, ๐šƒ> {
๐šƒ ๐š–๐šŠ๐š™(๐š‚๐š˜๐šž๐š›๐šŒ๐šŽ ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ);
}

๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐šŒ๐š•๐šŠ๐šœ๐šœ ๐šƒ๐šข๐š™๐šŽ๐Ÿธ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š› ๐š’๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šœ ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<๐šƒ๐šข๐š™๐šŽ๐Ÿธ๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š, ๐šƒ๐šข๐š™๐šŽ๐Ÿธ> {
@๐™พ๐šŸ๐šŽ๐š›๐š›๐š’๐š๐šŽ
๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐šƒ๐šข๐š™๐šŽ๐Ÿธ ๐š–๐šŠ๐š™(๐šƒ๐šข๐š™๐šŽ๐Ÿธ๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ) {
๐š›๐šŽ๐š๐šž๐š›๐š— ๐š—๐šŽ๐š  ๐šƒ๐šข๐š™๐šŽ๐Ÿธ(๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ.๐š๐š’๐šŽ๐š•๐š๐Ÿท, ... , ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ.๐š๐š’๐šŽ๐š•๐š๐™ฝ);
}
}

๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐šŒ๐š•๐šŠ๐šœ๐šœ ๐šƒ๐šข๐š™๐šŽ๐Ÿท๐™ผ๐šŠ๐š™๐š™๐šŽ๐š› ๐š’๐š–๐š™๐š•๐šŽ๐š–๐šŽ๐š—๐š๐šœ ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›<๐šƒ๐šข๐š™๐šŽ๐Ÿท๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š, ๐šƒ๐šข๐š™๐šŽ๐Ÿท> {
@๐™ฐ๐šž๐š๐š˜๐š ๐š’๐š›๐šŽ๐š @๐™ป๐šŠ๐šฃ๐šข
๐š™๐š›๐š’๐šŸ๐šŠ๐š๐šŽ ๐™ผ๐šŠ๐š™๐š™๐šŽ๐š›๐™ป๐š˜๐šŒ๐šŠ๐š๐š˜๐š› ๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐™ป๐š˜๐šŒ๐šŠ๐š๐š˜๐š›;

@๐™พ๐šŸ๐šŽ๐š›๐š›๐š’๐š๐šŽ
๐š™๐šž๐š‹๐š•๐š’๐šŒ ๐šƒ๐šข๐š™๐šŽ๐Ÿท ๐š–๐šŠ๐š™(๐šƒ๐šข๐š™๐šŽ๐Ÿท๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ) {
๐š๐š’๐š—๐šŠ๐š• ๐šƒ๐šข๐š™๐šŽ๐Ÿธ ๐š๐šข๐š™๐šŽ๐Ÿธ = ๐š–๐šŠ๐š™๐š™๐šŽ๐š›๐™ป๐š˜๐šŒ๐šŠ๐š๐š˜๐š›.๐š–๐šŠ๐š™(๐š—๐šŽ๐š  ๐šƒ๐šข๐š™๐šŽ๐Ÿธ๐™ฒ๐š˜๐š—๐š๐šŽ๐šก๐š(๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ.๐š๐š’๐šŽ๐š•๐š๐š‡, ๐š๐š’๐šŽ๐š•๐š๐šˆ));
๐š›๐šŽ๐š๐šž๐š›๐š— ๐š—๐šŽ๐š  ๐šƒ๐šข๐š™๐šŽ๐Ÿท(๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ.๐š๐š’๐šŽ๐š•๐š๐Ÿท, ... , ๐šœ๐š˜๐šž๐š›๐šŒ๐šŽ.๐š๐š’๐šŽ๐š•๐š๐™ฝ, ๐š๐šข๐š™๐šŽ๐Ÿธ);
}
}

This is the last thing needed to implement. Mappers are just mappers, well distinguished by the MapperLocator. By the way, they allow you to call the MapperLocator from inside for doing nested mapping.

๐—ฃ๐—ฅ๐—ข๐—ฆ

Simple to use, easy to increase mapping coverage, loose coupling, atomicity of mappers, easy testing and maintenance, and regular JVM garbage collection is efficient.

๐—–๐—ข๐—ก๐—ฆ

The entry-level requirements for developers are higher, requiring code reviews and proper test coverage.

๐—ฆ๐—จ๐— ๐— ๐—”๐—ฅ๐—ฌ

What is the way to use this solution once itโ€™s introduced? Simply add a ๐˜•๐˜ฆ๐˜ธ๐˜›๐˜บ๐˜ฑ๐˜ฆ๐˜”๐˜ข๐˜ฑ๐˜ฑ๐˜ฆ๐˜ณ, a ๐˜•๐˜ฆ๐˜ธ๐˜›๐˜บ๐˜ฑ๐˜ฆ๐˜Š๐˜ฐ๐˜ฏ๐˜ต๐˜ฆ๐˜น๐˜ต, and supply the new mapper to the ๐˜”๐˜ข๐˜ฑ๐˜ฑ๐˜ฆ๐˜ณ๐˜“๐˜ฐ๐˜ค๐˜ข๐˜ต๐˜ฐ๐˜ณ. Then call the new mapper via the mapperLocator's ๐˜ฎ๐˜ข๐˜ฑ() method.

๐˜–๐˜ณ๐˜ช๐˜จ๐˜ช๐˜ฏ๐˜ข๐˜ญ ๐˜ข๐˜ณ๐˜ต๐˜ช๐˜ค๐˜ญ๐˜ฆ: https://github.com/roman-vidayko/articles/blob/main/a001/src/content/article_data_mapping.txt

๐˜š๐˜ต๐˜ข๐˜ณ๐˜ต๐˜ช๐˜ฏ๐˜จ ๐˜ฑ๐˜ฐ๐˜ช๐˜ฏ๐˜ต ๐˜ต๐˜ฐ ๐˜ญ๐˜ฆ๐˜ข๐˜ณ๐˜ฏ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ค๐˜ฐ๐˜ฅ๐˜ฆ: https://github.com/roman-vidayko/articles/blob/main/a001/src/main/java/com/vidayko/service/CustomerApiService.java#L38-L53

Top comments (0)