小编Jan*_* M.的帖子

用于互连服务的ZF3服务DI的有效模式

据我所知,有效模式是:

  • 一个实例化所需服务的FooControllerFactory(FooService)
  • 一个带有构造函数__construct的FooController(FooService $ fooService)
  • Controller获取一些基本数据并从服务中获取结果
  • 服务包含所有必需的业务逻辑这是一个基本服务.最终,此服务将需要其他服务用于各种活动.例如CacheService,SomeOtherDataService.

问题是,包含/注入其他互连服务的有效/适当模式是什么?

我们目前的reallife示例极其简化:

AuctionController

/**
  * get vehicles for specific auction
*/
public function getVehiclesAction ()
{
    $auctionService = $this->getAuctionService(); // via service locator
    $auctionID = (int) $this->params('auction-id');
    $auction = $auctionService->getAuctionVehicle($auctionID);
    return $auction->getVehicles();
}
Run Code Online (Sandbox Code Playgroud)

AuctionService

public function getAuctionVehicles($auctionID) {
    $auction = $this->getAuction($auctionID);
    // verify auction (active, permissions, ...)
    if ($auction) {
        $vehicleService = $this->getVehicleService(); // via service locator
        $vehicleService->getVehicles($params); // $params = some various conditions or array of IDs
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

VehicleService …

php zend-framework-mvc zend-framework2 zend-framework3 zf3

4
推荐指数
1
解决办法
746
查看次数