• Post Reply Bookmark Topic Watch Topic
  • New Topic

should (custom) value objects always be immutable?  RSS feed

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i read about immutable objects and their advantages. further more i read that value objects should always be immutable.
following problem: let's say my application's domain model is Person with field of type Money (which can be changed by setAmount(...)).
in my view Money is a value object (just the amount counts to be identified). but to me there is still no reason to turn it to a immutable class:

-it is not shared by more than one object (1:1 relation Person<->Money)

what other reason could there be? maybe, "better" design of my domain model?

a little bit off-topic: i am using hibernate to map my domain-model to RDBMS. maybe i should definetely not use an immutable Money, because it would litter my database with loads of Money-rows?

thank you!
[ March 13, 2006: Message edited by: manuel aldana ]
 
Bimal Patel
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my personal openion, should value objects be mutable or not, decided by the circumstances driven by the whole system. In other words it all depends upon the need . But I think if your motive is to persist such objects, it should be immutable. Yes, you can rather read from the hibernate data objects and copy those to another presentation layer value objects. Such presentation value objects can be mutable. And yes, ofcourse, whenever you're going to update the values in the DB, you need to update the previously taken objects of hibernate with the new values you're getting from the presentation value objects.

This is going off the track but again, as I said, all it depends upon the NEED!
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a little more (and last) off track.

well, my domain-model is mapped directly by hibernate to db. i think i will make all my objects mutable, especially because my surrogate keys would make things more complicated, when creating a new immutable instance.

of course i agree on 'always to our needs'
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have your own Money table?
 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good question of course it is not an own table, its properties are columns in table Person (hibernate component).

i just omitted this (hibernate detail) for the sake of my general immutable question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!