I'm having a little trouble understanding how to implement best DI practices in this scenario.
I have a ZF1 project with the Symfony2 DI container implemented. All my service classes etc are created through the DI container and i'm now refactoring the dependencies of my service classes themself. So far so good but now I've stumbled upon a problem where I need to create X instances of a Doctrine Entity, yet I don't know how to abstract the dependency out of the service class into the container. It isn't a real single class instance so it can't be injected at construction time.
There are several scenario's that come to mind and i'm looking for advice what is the preferred road to take.
Get a reference to the DI container and fetch new instances from it whenever needed, this has the disadvantage that now my Service class will depend on the DI container which imho is fixing evil with more evil.
Call a factory, I've read about the factory implementation in the symfony DI container but fail to see how this could help me. Maybe I'm missing something?
Injecting the entity className and instantiating the instance dynamically. Thus instead of doing
new \My\Entity()
doingnew $this->entityClass
, this smells too to be honest but could work.??
Can anyone tell my how these scenario's are commonly solved using the symfony2 DI container?