Public Fields in DTO and Domain object
I'm doing a code review for one project. It's architecture you can se开发者_如何学运维e on the following scheme:
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?
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.
I would use JavaBeans because:
There are open-source helper classes that work with JavaBeans. e.g. Setting a property value whose name is unknown until the runtime.
There are times you need to do simple data transformation. Getters and setters can do that transparently.
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.
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.
精彩评论