• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between ValueObject and Domain object - in more details

 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do understand the differences between Value Object and domain object. There are a lot of references to it in Core J2EE pattern and other resources. However, it also depends on how Value Object and domain object are implemented.

I will appreciate clarification on the following:

1) Are the atrributes (member variables) in Value Object public? Some postings mention that the attributes in Value Object are made public, as opposed to making them private in Domain object. Is this correct?

2) Some postings also mention that Value Object has no method. It only has public attributes. Is this correct?

3) Value object may have all the attributes that the corresponding domain object has. In addition to that, Value Object may have more attributes. Is this observation correct?

4) All the attributes of Value object will get fully loaded, while the attributes of Domain object may not get fully loaded. Is this observation correct?
 
Gabriel Claramunt
Ranch Hand
Posts: 375
Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do understand the differences between Value Object and domain object. There are a lot of references to it in Core J2EE pattern and other resources. However, it also depends on how Value Object and domain object are implemented.

You can see value objects (or Transfer objects) as a mean to interchange data between layers. Usually have a one to one mapping with the domain objects, but no logic, as we want to avoid use of business logic in other layers.


I will appreciate clarification on the following:

1) Are the atrributes (member variables) in Value Object public? Some postings mention that the attributes in Value Object are made public, as opposed to making them private in Domain object. Is this correct?

VOs can have public attributes or private with accessors (get/set).
Depends on your preference/requirements... check the Transfer Object in the Core J2EE Pattern Catalog for the alternatives and what each one implies

2) Some postings also mention that Value Object has no method. It only has public attributes. Is this correct?

Not always... I prefer private attributes and set/get but it can be public attributes too.
In some cases get/set are preferred: if you want to create immutable objects, you set the values in the constructor and only provide getters


3) Value object may have all the attributes that the corresponding domain object has. In addition to that, Value Object may have more attributes. Is this observation correct?

As always, depends on the context (there is no right or wrong).
I'm not sure if I can think an example of a VO with more attributes than the domain object (but I guess it could be possible)
On the other hand, a domain object can have internal state information not required outside the domain layer, and therefore, it shouldn't be in the VO.


4) All the attributes of Value object will get fully loaded, while the attributes of Domain object may not get fully loaded. Is this observation correct?
Again, depends on the context... a partially loaded VO means more network traffic to complete it, on the other hand, if you have a complex VO structure with nested collections, there's no need to pass everything through the network to display only a name...

It's very hard to come with clear cut answers (a Software architect's life is not easy :-) )
 
Bob Grundelheim
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attributes in a java class should NEVER, NEVER, NEVER be public.
I don't care if it is a VO, DTO, etc.
This is a violation of the JavaBeans standard.
Always make your attributes private, and use public getters and setters in these types of objects.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65342
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"JavaBob JavaBob", please check your private messages for an important administrative matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic