3

I'm doing a code review for one project. It's architecture you can see on the following scheme:

system architecture

At this moment DTO - are simple POJO's and Domains - contains Hibernate anotations. As I know one of the benefits using DTO is that you can make all domain fields as public and remove all that stub code with getters and setters. Is this correct approach?

What do you think about removing getters and setters from DTO's too? Also may be there are some pros implementing DTO's in Groovy?

What do think about that?

trnl
  • 514
  • 3
  • 20

3 Answers3

5

I think that security modifiers still can do some useful work in DTO layer. The most of the fields surely can be marked as public, because the main purposes of it is to be simply set from presentation layer. But some could be set only in specific way or have some other special things.

So, making long story short, you can use public for simple fields.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
1

I would use JavaBeans because:

  1. There are open-source helper classes that work with JavaBeans. e.g. Setting a property value whose name is unknown until the runtime.

  2. There are times you need to do simple data transformation. Getters and setters can do that transparently.

  3. You can check data "syntax". e.g. Throw a NullPointerException if a field should never be null.

Either way, do not mix public fields with JavaBeans. It'll confuse the heck out of everyone.

Tom Tucker
  • 11,676
  • 22
  • 89
  • 130
  • 1
    1. Can you please clarify about what helper classes are you speaking about? Smth like BeanUtils? But as far as I know they haven't got problems working with public fields. 2. Simple data transformation - yes, but ideally dto's should be used only for transferring data and of course additional method's can be implemented. 3. Check data "syntax" - again, I think that this should be handled by DTO2Domain convertor. What do you think about that? – trnl Mar 19 '11 at 18:51
0

The only cons of making fields public in dto's is that this can bring some misunderstanding for other team members. With getters and setters you can type get* and wait until ide suggest you all fields.

trnl
  • 514
  • 3
  • 20