2

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.

0

2 Answers 2

5

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).

2
  • 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. May 7, 2013 at 16:20
  • 1
    @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, 2013 at 16:47
2

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.

2
  • 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, 2013 at 16:15
  • 1
    @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. May 7, 2013 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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