[TYPO3-mvc] Cloning properties in setter and getter methods of models (blog example)
Franz Koch
typo3.RemoveForMessage at elements-net.de
Sat Feb 12 01:06:48 CET 2011
Hey Thomas,
> Okay, then. But I'm asking myself - and you pointed me in that
> direction - if those objects (and I mean all objects passed to
> setters and returned by getters) should be cloned regardless of
> any special usage inside the methods, just because DDD tells us
> that we need to use e.g. a setter to explicitly add new objects,
> rather than using an already returned object.
I got your point, but maybe we should have a look what possible impacts
this would have:
-------------
// create a virtual instance of you
$person = $this->objectManager->get('Tx_Ext_Domain_Model_Person');
$person->setFirstName('Thomas');
$person->setLastName('Deinhamer');
$person->setAddress('Home sweet home');
$this->employeeRepository->add($person);
// and grab a existing one of me
$noob = $this->employeeRepository->findByLastName('Koch');
// now let's give us some money
$payment = $this->objectManager->get('Tx_Ext_Domain_Model_Payment');
$payment->setAmmount(10);
$payment->setRecipient($person); // 10 bucks for you
// whops, we forgot to add your accounting data
// let's do it here, but wait, the payment is holding a
// clone, so your clone in the payment won't get affected
$person->setAccountingData($newAccountingData);
// even this wouldn't work then
$payment->getRecipient()->setAccountingData($newAccountingData);
// now add your payment to the manager
$accountingManager->addPayment($payment);
// raise the payment for you, but wait, we already assigned the
// payment object to the manager and it stored a clone internally so
// you won't get the new ammount
$payment->setAmmount(1000000);
// now pass the manager to the service
$this->ebankingService->setAccountingManager($accountingManager);
// it's my turn now - just gimme all that shit you didn't want
$payment2 = clone $payment;
$payment2->setRecipient($noob);
// now add my payment - but wait, the manager is already assigned
// as clone to the service - so also no money for me
$accountingManager->addPayment($payment2);
// give us the money, now - but wait,
// we don't get some due to the clones
$this->ebankingService->processAccountings();
// and last but not least, your inital payment object won't even
// get updated
return $payment->isPayed();
--------------
Now do we really want just clones all over the place? Using clones
everywhere would also make chained calles pretty ineffective.
$foo->getBar()->getBaz()->setFoo('bar');
--
kind regards,
Franz Koch
More information about the TYPO3-project-typo3v4mvc
mailing list