相关疑难解决方法(0)

ServiceLocator,让我们在ZF2上下文中对它进行思考

根据马可的Pivetta看法这个,这个老问题 和我的回答的另一个问题

我在询问自己在Zend Framework 2应用程序中使用我们的服务的更好方法.

实际上我们可以ServiceLocatorAwareInterface结合使用ServiceLocatorAwareTrait.事实上在ZF3中,服务定位器将被移除在控制器中它们可能也会删除此接口,或建议不使用它的人,这是有道理的.

我看到我们的服务如何构建的唯一方法是:

不要在服务中使用ServiceLocator,请使用DependancyInjection.

问题是 :

有些项目非常大,你要么:

  • 一个工作流程的15个服务类.
  • 服务类有15个Dependancies.
  • 选择你的噩梦......

您可能需要在服务中使用的一些示例:

  • 取回formManager(你不能在控制器中调用它)
  • 在通过AJAX和JSON响应将HTML字符串返回到视图之前,您可能需要让ViewRenderer呈现模板;
  • 您可能需要取回ZF2提供的翻译或您想要的所有服务
  • 获取您的实体管理器,如果您有多个数据库,请在此处添加计数
  • 获取其他服务,如MailService,ExportService,ImportService等......
  • 如果你必须加载特定服务取决于客户端(BtoB中的多客户端网站...添加一些服务,因为你无法加载|调用AbstractFactory)

也许对于其中的一些观点,它们可以通过我不知道的技巧来解决.

我的问题是:

在一个服务中拥有15个或更多Dependancies并放弃ServiceLocator,在控制器中,还是在服务中,这是一个好习惯吗?

从评论中编辑

为了说明我的观点,我粘贴了一个构造函数:

public function __construct(
    ToolboxService $toolboxService,
    EntityService $entityService,
    UserService $userService,
    ItemService $itemService,
    CriteriaService $criteriaService,
    Import $import,
    Export $export,
    PhpRenderer $renderer
) {
    $this->toolboxService = $toolboxService;
    $this->entityService = $entityService;
    $this->userService = $userService;
    $this->emOld = $this->toolboxService->getEmOld();
    $this->emNew = $this->toolboxService->getEmNew();
    $this->serviceLocator = $this->toolboxService->getServiceLocator();
    $this->itemService = $itemService;
    $this->criteriaService = …
Run Code Online (Sandbox Code Playgroud)

php architecture zend-framework2

5
推荐指数
1
解决办法
142
查看次数

标签 统计

architecture ×1

php ×1

zend-framework2 ×1