Note that there are some explanatory texts on larger screens.

plurals
  1. POPattern to transform objects
    text
    copied!<p>I am looking for some best practices/patterns to transform objects from one to another. I load the data from DB using DAO to create my domain objects. In my application I need to transform into a different object that acts as an input to another module. For example, the customer is the domain object which holds a list of orders. I need to transform this into a object customerorder.</p> <pre><code>Public class Customer { String customerid List&lt;Order&gt; orders } Public class Order { Integer id Date orderDate } /** Transformed objects **/ public class CustomerOrder { string customerid Integer orderid } </code></pre> <p>Currently I have an interface CustomerDTO which has a method to return a list of CustomerOrder objects and a concrete class implementing the interface</p> <pre><code>Public interface CustomerDTO { List&lt;CustomerOrder&gt; getData(Date date) } Public class CustomerDTOImpl implements CustomerDTO { Private Customer customer Public CustomerDTOImpl(Customer customer) { this.customer = customer } Public List&lt;CustomerOrder&gt; getData(Date date) { ..... Code to loop through orders and create and return a list with matching order dates } } </code></pre> <p>For simple, transformations I don't need a DTO class but my transdormations are very complex and I would like to keep the transformation logic separate for different objects. Initially I had a transformer class which just loops through all the objects and create transformed objects but I don't think that was a good design and thought of this DTO. But I believe there are better ways to do it.</p> <p>Also I need to be able to use the DTO pattern on several objects. In this Customer was just one such object? I have 20 like diffent objects that I need to transform into their respective transformed objects.</p> <p>Any thoughts on best practices and pattern would be very useful. Also is there any generics I could use to scale them better.</p> <p>Thanks Javid</p>
 

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