[FLOW3-general] DependencyInjection: Best practice

Julian Kleinhans typo3 at kj187.de
Sun Jan 3 22:10:17 CET 2010


Hi Sebastian,

thanks for ur answer and the light in the darkness :-)
Performance and UnitTests are a good point!

cheers
julian



Sebastian Kurfürst schrieb:
> Hey Julian,
> 
>> 2) Setter injection
>> public function setTest(F3\Test $test) {
>>   $this->test = $test;
>> }
>>
>> or
>>
>> public function injectTest(F3\Test $test) {
>>   $this->test = $test;
>> }
> I am not sure that the set* method works. I'd always use the inject
> method in this case.
> 
>> What is different between 2 and 3? What is the best way ?
>> The property injection needs no method and is really short.. so i would
>> say, use property injection instead setter injection.. or is there any
>> special reason ?
> It depends. Inside the framework, we mostly use setter injection (via
> inject*), mostly to optimize performance (as this is a lot faster than
> @inject annotations inside the framework).
> Additionally, there is one reason for setter injection: It is a lot
> easier to test than property injection.
> 
> To test property injection, you need to do stuff like:
> 
> $myObject = $this->getMock($this->buildAccessibleProxy('myClass'),
> array('dummy'));
> 
> $myObject->_set('test', myTestObject);
> 
> So as some parts of FLOW3 are particularily difficult to test, that's
> another reason for using inject* methods at least inside FLOW3 - to make
> the testcases more understandable.
> 
> For my own application, I'd more often use the @inject annotation
> though, as it is more nicely readable, on the cost of a bit more complex
> unit tests.
> 
> Greets,
> Sebastian


-- 
Julian Kleinhans
Certified TYPO3 Integrator
email: typo3 at kj187.de
web:   http://www.typo3-tutorials.org

Xing: http://www.xing.com/profile/Julian_Kleinhans
Twitter: http://twitter.com/kj187


More information about the FLOW3-general mailing list