views:

44

answers:

1

Hello: I have an app where in a thread hierarchy (persisted entity) is modelled as follows (note that this could be a deeply nested hierarchy):

Thread
{
    private key;
    private rootKey;
    private parentKey;


.. getters ..
.. setters ..
}

I have a DTO which has the following structure

ThreadDTO
{
    private key;
    private rootKey;
    private parentKey;

    ArrayList<ThreadDTO> childThreads;

... getters ...
... setters ...
}

I would like to convert the entity instances into the DTO. Are there any standard algorithms / best practices available that can be optimally be used for doing this transformation ? Any feedback would be appreciated..

+1  A: 

DTO are quite annoying, the best thing you can do with them is to create a method which takes a Thread in parameter and copy the attributes, and another which will return a Thread with a copy of the attributes.

An other solution from Adam Bien is the Generic DTO, there is less security but also less copy/paste of in code.

Colin Hebert
My question was more related to how to create the nested hierarchy (i.e. population of the ArrayList<ThreadDTO>) so that the Top level ThreadDTO instances contain all the child thread DTO's (self referencing hierarchy)..
newbie
@newbie, same way, As you have to copy manually all the content of `Thread to `ThreadDTO` you just have to go through your List and get the `ThreadDTO` for each `Thread`. Be careful with cyclic references.
Colin Hebert
@Colin, thanks for the response. I was looking for a performance optimized way of populating the ArrayList childThreads
newbie