Note that there are some explanatory texts on larger screens.

plurals
  1. PODesign patterns for converting objects with nested lists
    primarykey
    data
    text
    <p>I have a service where the flow is basically the following:</p> <ol> <li>Receive an input object. This is just a POJO object and I don't have much say in the design of it.</li> <li>Convert to a normalized object for my service.</li> <li>Perform some business logic on the normalized object, and gather some extra data about it.</li> <li>Convert to an output object for another service that the data gets passed to. (Another POJO.)</li> <li>Pass the converted data to another service.</li> </ol> <p>What this means, though, is that a good portion of my service is converting from type <code>InputFoo</code> to type <code>NormalizedFoo</code> to type <code>OutputFoo</code>.</p> <p>This would be a pretty easy task. I'm using the Google Collections library and can have a class like this:</p> <pre><code>public class InputFooToNormalizedFooConverter implements Function&lt;InputFoo, NormalizedFoo&gt; { public NormalizedFoo apply(InputFoo input) { NormalizedFoo output = new NormalizedFoo(); output.setProperty(input.getProperty()); } } </code></pre> <p>and another class like this:</p> <pre><code>public class NormalizedFooFooToOutputFooConverter implements Function&lt;NormalizedFoo, OutputFoo&gt; { public NormalizedFoo apply(InputFoo input) { NormalizedFoo output = new NormalizedFoo(); output.setProperty(input.getProperty()); } } </code></pre> <p>But each type of Foo essentially has a hierarchy like so:</p> <pre><code>public class Foo { List&lt;Bar&gt; barItems; // .. other properties } public class Bar { List&lt;Baz&gt; bazItems; List&lt;Quux&gt; quuxItems; // .. other properties } public class Baz { // .. other properties } public class Quux { // .. other properties } </code></pre> <p>This means that I have <code>NormalizedFooToOutputFooConverter</code> that has a <code>NormalizedBarToOutputBarConverter implements Function&lt;NormalizedBar, OutputBar&gt;</code> type and so on and so forth.</p> <p>Even worse, the input doesn't <em>quite</em> match up exactly to the normalized model. It's more like</p> <pre><code>public class InputFoo { public List&lt;InputBar&gt; bars; public List&lt;InputBaz&gt; bazs; public List&lt;InputQuux&gt; quuxs; // .. other properties } public class InputBar { private String barId; // .. other properties } public class InputBaz { private String barId; private String bazId; // .. other properties } public class InputQuux { private String barId; private String quuxId; // .. other properties } </code></pre> <p>In these models, I can figure out which <code>Baz</code> and <code>Quux</code> belongs to which <code>Bar</code> based on the <code>barId</code> that each one has.</p> <p>At this point, I have about 20 different converters for going from <code>Input</code> to <code>Normalized</code> and <code>Normalized</code> to <code>Output</code>. And worse still, some of them have name like <code>ReallyLongInputTypeToReallyLongNormalizedTypeConverter</code> creating extremely long class names. I feel like I'm doing something wrong here, with all the converters. Is there a better way to organize my converters?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload