Valid template for ZF3 DI service for interconnected services

As far as I understand, a valid template:

  • FooControllerFactory, which creates the necessary services (FooService)
  • FooController with __construct constructor (FooService $ fooService)
  • The controller receives some basic data and receives the result from the service.
  • The service contains all the necessary business logic. This is a basic service. This service may require other services for various activities. For example, CacheService, SomeOtherDataService.

The question is, what is the right / appropriate template for including / injecting these other related services?

The reallife example we currently have is extremely simplified:

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();
}

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;
}

VehicleService

public function getVehicles($params) {
    $cache = $this->getCache(); // via service locator
    $vehicles = $cache->getItem($params);
    if (!$vehicles) {
        $vehicleDB = $this->getVehicleDB(); // via service locator
        $vehicles = $vehicleDB->getVehicles($params);
    }
    return $vehicles;
}

An example of the proposed template

AuctionController

public function __construct(AuctionService $auctionService) {
    $this->auctionService = $auctionService;
}

/**
  * get vehicles for specific auction
*/
public function getVehiclesAction ()
{
    $auctionID = (int) $this->params('auction-id');
    $auction = $this->auctionService->getAuctionVehicle($auctionID);
    return $auction->getVehicles();
}
**AuctionService**

public function getAuctionVehicles($auctionID) {
    $auction = $this->getAuction($auctionID); // no problem, local function
    // verify auction (active, permissions, ...)
    if ($auction) {
        $vehicleService = $this->getVehicleService(); // we don't have service locator
        $vehicleService->getVehicles($params); // $params = some various conditions or array of IDs
    }
    return false;
}

VehicleService

public function getVehicles($params) {
    $cache = $this->getCache(); // we don't have service locator, but cache is probably static?
    $vehicles = $cache->getItem($params);
    if (!$vehicles) {
        $vehicleDB = $this->getVehicleDB(); // where and how do we get this service
        $vehicles = $vehicleDB->getVehicles($params);
    }
    return $vehicles;
}

Some notes:

  • , 95%
    • , Vehicle
    • VehicleController VehicleService, , , .
    • , ( , ).
  • - , , , .
+4
2

, , .

@AlexP . dependecy . , , , ( , ).

, , Service Manager lazy services . / ( ), .

:

$serviceManager = new \Zend\ServiceManager\ServiceManager([
    'factories' => [
        Buzzer::class             => InvokableFactory::class,
    ],
    'lazy_services' => [
         // Mapping services to their class names is required
         // since the ServiceManager is not a declarative DIC.
         'class_map' => [
             Buzzer::class => Buzzer::class,
         ],
    ],
    'delegators' => [
        Buzzer::class => [
            LazyServiceFactory::class,
        ],
    ],
]);

:

$buzzer = $serviceManager->get(Buzzer::class);

:

$buzzer->buz();

, , . , , , ..

+5

, VehicleAuctionService AuctionService, VehicleService , factory.

.

class VehicleAuctionService
{
    private $auctionService;
    private $vehicleService;

    public function __construct(
        AuctionService $auctionService, 
        VehicleService $vehicleService
    ){
        $this->auctionService = $auctionService;
        $this->vehicleService = $vehicleService;
    }

    public function getAuctionVehicles($auctionID)
    {
        $auction = $this->auctionService->getAuction($auctionID);

        if ($auction) {
            $params = [
                'foo' => 'bar',
            ];
            $this->vehicleService->getVehicles($params);
        }
        return false;
    }

}
+1

All Articles