Symfony2依赖项注入容器子容器 [英] Symfony2 dependency injection container sub-container

查看:98
本文介绍了Symfony2依赖项注入容器子容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将某些服务放在另一个容器中,该容器将缩小到特定的服务集?还是将某些特定服务分离​​到某种子容器中?我问这个问题是因为我需要将一组服务注入到另一个服务中,并且将整个容器投入服务是我的主意,这是一个坏主意。当然,我可以在服务类中为要注入的所有服务创建一个属性,但这是我要避免的解决方案。

Is it possible to put some services in to another container which will be narrowed to a specific set o services? Or to separate some specific services to some kind of sub-container? I am asking this question because I need to inject a set of services into another service, and putting a whole container into service is , I belive, bad idea. Of course I can create a property in my service class for all services that I want to inject, but this is a solution that I am trying to avoid.

我们将不胜感激,如果您需要更多有关此问题的信息,请询问。

I will appreciate any help, and if you need more info about the problem just ask.

推荐答案

出于性能和可测试性的原因,确实不建议注入整个容器。

Injecting the whole container is indeed not recommeded for performance and testability reasons.

如果不是所有依赖项的构造方法或getter / setter注入方式,您都不希望...

If constructor or getter/setter injection for all dependencies is not the way you want to go...

... a 服务工厂是您要寻找的东西。

... a Service Factory is what you're looking for.

这样,您可以构建包含其他服务的服务,并仅注入该服务。

This way you can construct a service holding your other services and inject only this one.

那将是您所说的子容器。

That would be kind of the "subcontainer" you are talking about.

这篇关于Symfony2依赖项注入容器子容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆