[FLOW3-general] DependencyInjection: Best practice
Sebastian Kurfürst
sebastian at typo3.org
Sun Jan 3 22:02:52 CET 2010
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
More information about the FLOW3-general
mailing list