Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a DAO that brings data from a web service, that data comes in a string, in the likes of:

*NAME|John Doe *DATEOFBIRTH|1978-23-01*ID|anID123 (...)

I have a DTO that I wanna fill up with the parsed data (the DTO has fields in the like of name, birthdate, id...)

So, my question is, what is the best practice:

  • Parsing the string in the DAO to fill up the DTO
  • Passing the data string to the DTO and the DTO itself parse the string and fill itself up
  • Do the parsing in an utility class that would receive the string and return the fill up DTO

DAO: Data Access Object

DTO: Data Transfer Object

Note: I kept this language agnostic so it could be useful to anyone using object oriented languages, but, in my particular case, I'm using Java.

share|improve this question
up vote 5 down vote accepted

Typically an unmarshalling construct can be used.

The job of the unmarshaller would be simply to translate from the string to your domain specific DTO. Your DAO or DTO will not need to change if the transmission format changes. Additionally, the unmarshalling strategy can be mocked out for unit tests (and tested separately). Depending on your enterprise architecture pattern, you can invoke the unmarshaller anywhere (in the DAO, the DTO, using AOP, ...).

Many widely used software packages use the marshalling/unmarshalling concept including Spring, JAXB, and the AWS Java SDK. I suggest that you take a look at these libraries to get some ideas as to how you can implement and fit into your design (you may find a ready made solution that fits your needs).

share|improve this answer
Ack. You had me until I realized you linked to a Wikipedia article, so this is general advice; I now have to figure out what you mean by unmarshalling in this specific context. Your answer would benefit greatly from an example in Java. – Robert Harvey May 7 '13 at 16:20
@RobertHarvey Unmarshalling has been done and redone. I prob won't do it justice in a few lines on stack exchange so I linked to some well known examples to try and satisfy your concern. – smp7d May 7 '13 at 16:47

The DTO should be responsible for filling up its own fields from the parsing result, because the fields are specific to the DTO.

The parsing itself can take place elsewhere, but Java has a Split() function, so your DTO already knows how to parse it. So just hang a Parse(string) function onto your DTO.

share|improve this answer
I kind of agree but to play devil's advocate. DTO's can be used at business and UI layers. Is parsing relevant at layers other than the DAL? Should someone be able to see a Parse() method while they using the DTO for business purposes? In this case it's a string, but Parse could also be applied to a layer-specific JDBC type like a ResultSet. – mike30 May 7 '13 at 16:15
@mike30: To do this in a more general way you have to Reflect over the DTO and map the fields to the incoming parsed fields. To do that, you need a mapping of the fields to be parsed, which I don't see here. It's been done before, but I would use a preexisting library rather than trying to write that myself. – Robert Harvey May 7 '13 at 16:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.