开发者

Is it wrong to use Service Locator to provide the container instance to a Service?

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 Servic开发者_如何转开发e.

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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜