经常注入服务容器是一种不好的做法

Is injecting service container very often a bad practice

我有一些服务,它们在其他服务中也有一些依赖关系,因此,我决定在这些服务中的每一个中注入 @service_container,而不是非常具体的依赖关系。结果是这样的:

class InternalComponentHelper implements ContainerAwareInterface
{
    use ContainerAwareTrait;

   public function somefunction(){
       //Do something with the container
   }
}

然后在我的服务定义中

      intcomponent_helper:
        class: AplicacionBaseBundle\DependencyInjection\Helpers\InternalComponentHelper
        calls:
          - [setContainer, ["@service_container"]]

我需要知道的是,这是否是一种不好的做法,它会造成什么危害或性能问题(如果有的话)。顺便说一句,没有循环依赖。

注入服务容器被认为是不好的做法。根据这个blog post

As easy as this seams to be, using the container directly is not considered a good practice because it hides the dependencies of your classes, making them coupled to external configuration, thus harder to test, harder to review, etc.