DAO: DTO necessary?

My application isn't distributed. I'm not using EJBs. For this case, is there any advantage of using data transfer objects when implementing the DAO pattern? I'm thinking of just passing the business object directly to the DAO to have it populated.Mike
[269 byte] By [magic886a] at [2007-9-29 20:51:43]
# 1

In the J2EE design patterns, DTO's are used to keep the network traffic to a minimum in distributed environments. If you are not planning to go into the direction of a distributed system with EJB's, etc, then no harm is done when you pass your business object directly to the DAO.

Cheers,

--Arnout

ajkuipera at 2007-7-16 1:01:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Arnout,

::then no harm is done when you pass your business object directly to the DAO

Dependency-wise, the Presentation layer depends on the Business Layer

which itself depends on the Data Access Layer.

So the fact that the DAO (part of Data Access Layer) depends on the BO (part of Business Layer)

is not good from the dependency pt of view. I'd prefer using a DTO so that DAO would not

depends on the BO (part of Business Layer). Is this preferable?

PaulPhuoca at 2007-7-16 1:01:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

I agree with you that DTO's decouple layers, and thus reduce dependencies, and therefore should be used when possible.

But in most projects you have to balance between 2 things, a beatiful decoupled future-proof extendable design and money. In large projects I would go for DTO's, because they earn themselves back due to the reduced complexity of the design. But in small projects, using DTO's can give an overhead that is too large, moneywise. You have to prevent that half of your code consists of copying data from/to DTO's; if this is the case, you are doing something wrong. I know it won't be that extendable, decoupled, etc, but sometimes these things are not required.

Cheers,

--Arnout

ajkuipera at 2007-7-16 1:01:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Arnout ,

::But in small projects, using DTO's can give an overhead that is too large, moneywise.

Overhead or performance may be.

But moneywise is not an real issue here because if we don't do it right in a small project then don't even bother about the large. But even if money is an issue for a small project, then does it mean we have to

give up dependency. Having a Data Layer depends on the Business Layer is against design

principle. So why bother justifying that it's acceptable in a small projects?

When we don't do it right in the first place, it will cost us more money in the long run.

::You have to prevent that half of your code consists of copying data from/to DTO's; if this is the case, you are doing something wrong.

I'm looking at this question (OP) or in general from the design principle point of view.

See my previous point.

::I know it won't be that extendable, decoupled, etc, but sometimes these things are not required.

Well, when we have a small projects or large, we have to think about maintainance as well.

And having a Data Layer depends on the Business Layer could cause trouble for maintainance.

PaulPhuoca at 2007-7-16 1:01:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Arnout and Paul,

Thank you for your replies so far. I do realize the trade-off would be a tighter coupling between the Business Layer and the Data Access Layer as you guys have mentioned. But, let's get down to specifics and what occurs in practice.

Could you list specifically, what this decoupling can buy you? And from your experience, in practice, how often would this occur?

Let me throw out some items:

1) Reuse of the DAO objects - Say you create a different application with different business objects and they need the same database queries, maybe you can reuse the same DAO objects. But, does that happen often? If there's reuse, wouldn't you most likely reuse the business object as well as the related DAO object?

2) If you make a change in the business object, maybe it'll prevent a change in the DAO object. I'm not sure if would be the case.The DAO object as I would use it would mainly call the getters and setters from the business object to manipulate the fields of the business object. If I change the name and types of the business object fields, I would need to now make a corresponding change in the DTO object and then the DAO object.

So, could you tell me from your experience what are some real benefits that occur often in practice that the decoupling can buy you.

Thanks,

Mike

magic886a at 2007-7-16 1:01:24 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

Hi Mike,

1)

::But, does that happen often?

You're the only one who know this for your specific requirement

in that I meant we do not know the circumstance of your business.

2)

::If I change the name and types of the business object fields, I would need to now make a corresponding change in the DTO object and then the DAO object.

This is what Arnout already stated which is implied money-wise concern he has.

::The DAO object as I would use it would mainly call the getters and setters from the business object to manipulate the fields of the business object.

If a web Tier (Servlet) pass the Request object which is a Web Tier object to Business Tier (BO)

to extract form data like UserName and Password then the Bus Tier depends on the Web Tier

which is not desirable since it's highly coupled (Why should BO depends on UI).

This is exactly what we have in your case: BO pass itself to DAO to extract data.

If a Swing Tier needs to reuse that BO, then it need to pass a Request object which is

impossible.

The web tier should extract Username and password and pass it to BO. This

way BO will not depend on Web tier. So a Swing Tier can reuse a BO as well.

This is the same a having a BO that pass a DTO to DAO which is design-wise preferable (Paul)

but money-wise not preferable (Arnout).

So it looks like design is a question of trade-off between reality and theory.

May be other people point of view are needed to see.

PaulPhuoca at 2007-7-16 1:01:24 > top of Java-index,Other Topics,Patterns & OO Design...