Re: Re: Nested immutable value objects

Giacomo Tesio <[email protected]>
Newsgroups gmane.comp.programming.domain-driven-design
Message-ID <CAHL7psFSiu=vG73xihmiUjLnjkt9uCJ9XGybrV2vp88zpin91g@mail.gmail.com>
I agree that this is not a good example, so lets suppose that the car is an
entity.

Setters are huge smell in DDD (and to my money, in OOP in general).
In the Car entity I would expose a
command<http://epic.tesio.it/doc/manual/command_query_separation.html>
 like

    car.GasUp(new Liter(1))

that would fire proper events and throw proper exceptions (eg
TankOverflowsException).

Just to make the example cleaner for value objects, let suppose that the
whole car state is modeled with an immutable object named CarState (I have
no imagination... sorry :-D).

Than, in car.GasUp I would write (at worst)

    state = state.WithTank(tank => tank.WithContent(content => content +
liters))

(this supposing that Tank and Content are both value objects, with Content
being of type Liter).

Of course, in real world, you should simply write:

    state = state.WithTank(tant => tank.IncreaseContent(liters))

with IncreaseContent taking a Liter and returning a new Tank. The previous
example was written that way to show how weird this approach could be with
deeper nests (that however do not occur often).


Hope this helps


Giacomo

On Tue, Mar 19, 2013 at 5:01 PM, Tom Eugelink <[email protected]> wrote:

> **
>
>
>
> I'm not sure this is a good example. I mean, in real life a car is mutable
> like hell; if you put in a new engine, the car remains the same. I figure
> immutability is more focus to primary types. For example volume (tank) or
> distance (drive).
>
> So suppose you have a car, with 10 liters of fuel in the tank.
> car.getTank().getContents() could return an object Liter(10)
>
> If you take out one liter from that tank, then this is is not how to do
> thiat:
> car.getTank().getContents().substract(1)
>
> This would be a better way, given the volume being immutable:
> car.getTank().setContents( car.getTank().getContents().substract(1) )
>
> IMHO
>
>  
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.