1

I'm using a Service layer, then, I've alot of Service like:

  • UserService
  • ArticleService
  • CommentService
  • AuthorizationService

Sometimes I need to use a Service from another Service.

Currently, I'm using sfServiceContainer for dependency injection, and I inject my Doctrine 2 Entity Manager in some of my service, however, I'm thinking to switch and inject the Container Instance to be able to fetch any Service and the EntityManager.

But I'm stuck and I'm not sure it's a good idea.

JohnT
  • 967
  • 2
  • 16
  • 30
  • possible duplicate of [Is it better to create a singleton to access unity container or pass it through the application?](http://stackoverflow.com/questions/2386487/is-it-better-to-create-a-singleton-to-access-unity-container-or-pass-it-through-t) – Mark Seemann May 15 '11 at 14:10

1 Answers1

0

It's ok to use a Service Locator if your container does not provide you with a better way to do this.

If you use a container like Ding (http://marcelog.github.com/Ding), your services would be beans, and instead of having a service locator, you can implement the IContainerAware interface in these services.

The container will then inject itself whenever these beans are created, and you can then $this->getBean('aService') directly from inside these services/beans. In the case of Ding, there's even an example of Doctrine2 integration (including the injection of the EntityManager)

marcelog
  • 7,062
  • 1
  • 33
  • 46