2012-09-19 32 views
4

我原來在幾個月前就有關ZF2在Beta 1期間向DI注入數據的問題做了另一個線索,並且認爲它不是真正可行的。現在ZF2已經發布爲2.0.0版本,並且ServiceManager默認不是DI我想我現在有同樣的問題,我正在重構。ZF2 - ServiceManager注入到84個表中...單調乏味

我有84張桌子,需要DbAdapter注入他們,我敢肯定,我必須有更好的方式來複制自己。

public function getServiceConfig() 
{ 
    return array(
     'factories' => array(
      'accountTable' => function ($sm) { 
       $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
       $table  = new Model\DbTable\AccountTable($dbAdapter); 
       return $table; 
      }, 
      'userTable' => function ($sm) { 
       $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); 
       $table  = new Model\DbTable\UserTable($dbAdapter); 
       return $table; 
      }, 
      // another 82 tables of the above 
     ) 
    ) 
} 

通過EventsManager和ServiceManager,我不知道我的位置在獲取我的應用程序的實例/資源。

感謝,大教堂

回答

2

隨着大量像這樣的注射,你最好把創建服務經理initalizer,然後執行類似DbAdapterAwareInterface的接口。如果你想看到這個想法,請查看zf2代碼庫中的EventManagerAwareInterface。

+0

嗯,這似乎是硬編碼到框架中。在Zend \ Mvc \ Service \ ServiceManagerConfig.php#configureServiceManager($ sm){serviceManager-> addInitializer(// stuff) } 因此,這將偵聽使用AwareInterface創建任何內容並將其注入到那裏。我想我必須添加我的DbAware作爲初始化器 –

-1

它確實沒問題。 php5.3 +只注入*指針或「鏈接」

0

我寫了一個名爲DiWrapper的ZF2模塊,您可以使用它來自動生成並自動更新此類工廠代碼。您將不得不將Zend\Db\Adapter\Adapter作爲共享實例提供給DiWrapper。然後,您可以使用ServiceManager或DiWrapper來獲取您的表類。

相關問題