Re: Nested immutable value objects
Giacomo Tesio <[email protected]>
| Newsgroups | gmane.comp.programming.domain-driven-design |
|---|---|
| Message-ID | <CAHL7psFy4_WxPMnmAc3F2xL_704Of-zDyeQx+PmL_pPddhALtg@mail.gmail.com> |
In C# I used delegate functions. Something like car.WithFluel(fluel => fluel.Increase(100)), with Car.WithFluel() returning a new Car, and Fluel.Increase returning a new Fluel. Car.WithFluel is responsible to mix the variation in the values. This works well as far as you don't have many levels of nested value objects: on the real world examples that I've met till now, I've needed at most 3 levels, and no one encountered problems while reading the code. Just remember to document all exceptions that each method does not handle (see http://epic.tesio.it/2013/03/04/exceptions-are-terms-ot-the-ubiquitous-language.html ). Giacomo On Tue, Mar 19, 2013 at 7:19 AM, sudarshan89 <[email protected]> wrote: > ** > > > am trying to wrap my head around modelling with immutable objects, while > doing so I am trying to understand how one would "update" a nested > immutable object. > > I will just present my case with a help of an example, A car is filling > fuel, this could be realized in code by creating a new car instance with a > added fuel (keeping the immutable objects in mind, i do not just want to > increment a counter) . > > So each time fuel is added I get a new car which has all the properties of > the old car but the fuel tank has more fuel. > > This sounds conceptually good however we I try to code it down. My car > would have a engine, tires and other immutable components, when a new car > is created the entire graph needs to re created and then put into this new > car, this would mean that i would need some kind of a copy constructor > within my car class. > > I was curious to know how such a case can be handled cleanly ... > > Thanks > > >