Fog Creek Software
Discussion Board




DAOs and TransferObjects (Core J2EE Patterns)

I'm currently reading Core J2EE Patterns (2nd edition) and was reading about Data Access Objects.

These make a lot of sense to me as they provide a good way of hiding data access code from other parts of an application. What confused me was that this book suggested passing and/or returning TransferObjects to/from DAOs.

If I understand it correctly, the authors are suggesting that in the case of a business object "Customer", database access should be handled by CustomerDAO and CustomerTO should be passed/returned to/from the DAO.

Why take this approach and not simply pass/return Customer/a collection of Customers?

Walter Rumsby
Sunday, August 10, 2003

Customer is likely to encapsulate a lot of Customer related behavior.  All you want to move is the state, not the behavior.

anon
Sunday, August 10, 2003

Hmm,

That makes sense - although what is the overhead per instance of "all that" behaviour (i.e. they're just method definitions)?

I understand the purpose of the TransferObject if you're communicating remotely, but isn't it just adding a layer it I'm using POJOs?

Walter Rumsby
Sunday, August 10, 2003

Check out The Server Side for a full discussion on patterns and their pro's and con's. http://www.theserverside.com/patterns/index.jsp

Regs,

James Ladd
Sunday, August 10, 2003

If your app isn't communicating remotely then TransferObjects are just an extra layer. But good J2EE patterns are designed to work well in the remote case.

Having said that, there are plenty of cases where it doesn't hurt to simplify this pattern. If you can get adequate performance passing an array of Customer objects up from your DAO then you don't really need a transfer object. But if the Customer object is too big, that's where this pattern solves a problem.

Beth Linker
Monday, August 11, 2003

A little tangental information, if you do decide to go with a design that has separate TransferObjects and business objects, have a look at the Commons BeanUtils package over at Apache Jakarta. It provides some useful methods to copy the contents of one bean object to another, thereby reducing a lot of the drudge work of copying between your business objects and your TransferObjects.

Bill Tomlinson
Monday, August 11, 2003

*  Recent Topics

*  Fog Creek Home