gpt4 book ai didi

performance - Symfony2 依赖注入(inject) : performances impact

转载 作者:行者123 更新时间:2023-12-04 02:42:17 31 4
gpt4 key购买 nike

我正在重构我的一个 Controller 以使其成为一项服务,我想知道不将整个服务容器注入(inject)我的 Controller 是否会对性能产生影响。

这样效率更高吗:

innova.path.controller:
class: %innova.controller.path.class%
arguments:
entityManager: @doctrine.orm.entity_manager
session: @session
securityContext: @security.context
router: @router
translator: @translator
pathManager: @innova.manager.path_manager
calls:
- [setRequest, ["@?request="]]
scope: request

比这个,例如?

innova.path.controller:
class: %innova.controller.path.class%
arguments: [@service_container]

最佳答案

官方文档明确告诉不要将整个 DIC 注入(inject) Controller (感谢@NHG 提供链接)。

How to work with scopes :

Injecting the whole container into a service is generally not a good idea (only inject what you need).

但在 Service container 部分:

When you ask for the my_mailer service from the container, the container constructs the object and returns it. This is another major advantage of using the service container. Namely, a service is never constructed until it's needed. If you define a service and never use it on a request, the service is never created. This saves memory and increases the speed of your application. This also means that there's very little or no performance hit for defining lots of services. Services that are never used are never constructed.

因此将整个 DIC 注入(inject) Controller 不会对性能产生影响,因为只有 Controller 中使用的服务被实例化。

关于performance - Symfony2 依赖注入(inject) : performances impact,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19657243/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com