我目前有一個ZF2模塊需要訪問三個數據庫表。沒有其他模塊需要訪問這些表。ZF2 SM,我真的需要
所以我的問題是(考慮到性能也請)應我真的工廠添加到Module.php就像我一直在做:
/**
* Service Configuration
*
* @return array
*/
public function getServiceConfig()
{
return array(
'invokables' => array(
'Login\Service\Authenticate' => 'Login\Service\Authenticate',
'Login\Service\Oauth' => 'Login\Service\Oauth'
),
'factories' => array(
'Login\Form\Login' => function() {
$form = new Form\Login();
$form->setInputFilter(new FormFilter\Login());
return $form;
},
'Login\Model\GaclEmployeePermission' => function ($sm) {
return new Model\GaclEmployeePermission($sm->get('OTWebsoft\Db\Adapter\Master'), $sm->get('OTWebsoft\Db\Adapter\Slave'));
},
'Login\Model\Passport' => function ($sm) {
return new Model\Passport($sm->get('OTWebsoft\Db\Adapter\Master'), $sm->get('OTWebsoft\Db\Adapter\Slave'));
},
'Login\Model\PassportLog' => function ($sm) {
return new Model\PassportLog($sm->get('OTWebsoft\Db\Adapter\Master'), $sm->get('OTWebsoft\Db\Adapter\Slave'));
}
)
);
}
然後,我有一個抽象類,它的作用:
/**
* Table GaclEmployeePermission
*
* @return \Login\Model\GaclEmployeePermission
*/
protected function getTableGaclEmployeePermission()
{
return $this->getServiceManager()->get('Login\Model\GaclEmployeePermission');
}
或者我應該剛剛從Module.php在我的抽象類中刪除配置只是這樣做:
/**
* Table GaclEmployeePermission
*
* @return \Login\Model\GaclEmployeePermission
*/
protected function getTableGaclEmployeePermission()
{
return new GaclEmployeePermission($this->getMasterAdapter(), $this->getSlaveAdapter());
}
兩者似乎都完全一樣。但是當涉及到性能時,你會推薦哪個?請記住,這三個表格不需要從任何其他模塊訪問,而是從這個模塊訪問。
我從模塊 - >控制器 - >服務層 服務層擴展了一個實現SM和其他功能的抽象類。我爲每個模塊提供了多個服務層,以便將代碼更多地拆分並使其易於識別(即:添加/編輯/刪除)。通過這種方式,每個圖層都可以擴展抽象類,而不必每次將所需的特徵添加到每個圖層。 因爲這個特定的模塊是唯一調用這些表的模塊,所以在合併的SM中配置較少會使事情變得更加輕量級。 – Diemuzi
那麼,如果每個實體都有一個服務,只需將數據庫適配器注入您的服務類中並將該服務注入到您的控制器中。您只需要進行一次注射,並使代碼保持清晰。 –