views:

15

answers:

0

Domain Driven Design (DDD) suggests using immutable objects or to copy object state when moving data around.

I see both good reasons for copying when setting data and when getting data.

Take the following interfaces and pretend they have implementations:

interface Aggregate {
    function setEntity($e);
    function getEntity();
}

interface Entity {
    function copy();
    function setValues($v);
    function getValues();
}

Then we have some facilitating factory code some place:

$e = new Entity();
$e->setValues("foo");

$a1 = new Aggregate();
$a2 = new Aggregate();

$a1->setEntity($e);
$a2->setEntity($e);

And then we might want to use the aggregated values some place:

# I know this is quite odd in DDD and probably makes no sense,
# but bear with me on this one
$e = $a1->getEntity();
$someething_i_dont_control->doSomeWork($e);

Would it make sense to copy on set if Aggregate if supposted to alter the internals of Entity? If not, then copy on get?

How about copy on both set and get, or would that be just too much processing for no fun?