views:

309

answers:

1

I'm looking for feedback on the Data Access Object design pattern and using it when you have to access data across multiple tables. It seems like that pattern, which has a DAO for each table along with a Data Transfer Object (DTO) that represents a single row, isn't too useful for when dealing with data from multiple tables. I was thinking about creating a composite DAO and corresponding DTO that would return the result of, let's say performing a join on two tables. This way I can use SQL to grab all the data instead of first grabbing data from one using one DAO and than the second table using the second DAO, and than composing them together in Java.

Is there a better solution? And no, I'm not able to move to Hibernate or another ORM tool at the moment. Just straight JDBC for this project.

+3  A: 

I would agree with your approach. My DAOs tend to be aligned more at the object level, rather than from a DB Table perspective. I may manage more than one object through a DAO, but they will very likely be closely related. There is no reason not to have SQL accessing two tables living in one DAO.

And for the record, I have banished the acronym DTO from my vocabulary and code.

pkananen
"I have banished the acronym DTO from my vocabulary and code." Can you explain more?
Casey
I just don't see the point to call an object a 'Data Transfer Object'. I populate domain objects directly in my DAOs, use them in my services, and expose them in my views (sometimes I may create alternate view objects). DTOs typically have no behavior, and are dumb property holders. I don't see a reason to limit my objects thusly in a modern Java project architecture. And by modern, I generally mean non-EJB, with a framework like Spring.
pkananen
I see. Pretty much the same way I use them. Thanks.
Casey
Is this a best practice? So in a DAO, you would have getWhatever() which calls getSomethingElse() to construct Whatever. I feel that this is error prone because the ordering is very important in both inserting and updating.
mangoDrunk
Can you give an example of why this is error prone? I'm having a hard time visualizing it.
pkananen