[TYPO3-mvc] Benefit of DI when referencing to a class (and not to an interface)?

Bastian Waidelich bastian at typo3.org
Tue Jun 26 17:42:53 CEST 2012


Roland wrote:

>> Yes, it's easier to create unit tests for classes that have less
>> dependencies.

> do you really reduce dependency with this?

Not literally, but you inverse the control [1] - so instead of creating 
an object, you ask the system for one.


> /**
>   * @var Tx_Foo_Domain_Repository_BarRepository
>   * @inject
>   */
> protected $barRepository;

> aren't you still dependent on a class named
> Tx_Foo_Domain_Repository_BarRepository?

Right, of course you can't prevent dependency altogether. But you should 
avoid too much of it as it makes code more complex and less reusable 
(think of your notification service - if it has a dependency to your 
BeerEntity, you couldn't reuse it for WineEntities) ;)
In general try to reduce the knowledge one module needs to have about 
the rest of the system (see [2]).


> maybe is should not worry that much about terms and definitions?


Right, do what works best for you and the requirements. On the other 
hand it's often a great help to learn something about software patterns 
and principles.


[1] http://en.wikipedia.org/wiki/Inversion_of_control
[2] http://en.wikipedia.org/wiki/Law_of_Demeter

-- 
Bastian Waidelich
TYPO3 Core Team Member

TYPO3 .... inspiring people to share!
Get involved: typo3.org




More information about the TYPO3-project-typo3v4mvc mailing list