views:

61

answers:

1

Here's what I know so far:

  • DI lets me build reusable, unit-testable components

  • DI is verbose because it requires that I explicitly set the dependencies (via constructor or method. I still don't understand the interface injection though). This is why a container or a service locator is needed.

  • Container is better than service locator because classes won't need to be aware of the existence of it.

But I found these problems:

  • Some classes will now depend from Container? If I don't use the default config for every class, as described in my services file, some classes will need to call the container to re-configure the needed object.

  • On page 79 from this slide http://www.slideshare.net/fabpot/dependency-injection-with-php-53, Fabien Potencier said that a Container does not manage all the objects, only those with a single instance (yet not singletons). I'm even more confused now.

Any help is greatly appreciated. =)

A: 

Some classes will now depend from Container?

No. That's why you use dependency injection as opposed to service location.

On page 79 from this slide...

See page 82, it says "Unlike model objects". Honestly I'd never explain it like that ("Objects with only one instance (!= Singletons)" is either wrong or something very PHP specific, it doesn't apply to dependency injection or IoC+DI containers in general), but I bet what he was trying to explain is that the container usually manages service-like things, not model-like things.

Mauricio Scheffer
If they don't depend from container, then how do they create childs with non-default configurations (ie: runtime defined settings) ?
HappyDeveloper
@HappyDeveloper: it's the container who does all wiring and configuration. Individual classes *receive* dependencies and configuration.
Mauricio Scheffer
Yea I know, but that works only for default object creation. What if at some point of the program execution (inside a child's child's child's method), for some reason, I need a say, DB_Cache dependency, instead of File_Cache ? I need to call the Container
HappyDeveloper
@HappyDeveloper: nope, if a component needs a different implementation of some service, it's up to the container configuration to serve the correct implementation.
Mauricio Scheffer
1 - The Container reads the config file 2 - The Container wires everything up 3 - My app keeps executing, and at some point an 'if' statement is executed, and an instance of A or B is created. 4 - Container predicted the future so B (which I wanted) was already wired? Also, sometimes I may want to avoid instantiation (eiter of A or B) . I don't think this is going to work
HappyDeveloper
@HappyDeveloper: In some situations you might want to inject a container-provided factory. But never the container itself.
Mauricio Scheffer
@HappyDeveloper: the last comment was a question out of the scope of the original question. Please create additional questions instead.
Mauricio Scheffer
Out of scope? It wasn't even a question, I was just trying to make you understand my problem.Yeah, injecting a factory seems to be the most common solution, but it feels like I wasn't using a container in the first place. Just good old GOF approach.
HappyDeveloper
@HappyDeveloper: it's not quite the same. I recommend getting a book on dependency injection.
Mauricio Scheffer