i have zf2 module needs access 3 database tables. no other module needs access these tables.
so question (performance in mind too, please) should add factories module.php have been doing:
/** * 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')); } ) ); }
then have abstract class does:
/** * table gaclemployeepermission * * @return \login\model\gaclemployeepermission */ protected function gettablegaclemployeepermission() { return $this->getservicemanager()->get('login\model\gaclemployeepermission'); }
or should remove configuration module.php , in abstract class this:
/** * table gaclemployeepermission * * @return \login\model\gaclemployeepermission */ protected function gettablegaclemployeepermission() { return new gaclemployeepermission($this->getmasteradapter(), $this->getslaveadapter()); }
both seems work same. when comes performance, recommend? keeping in mind, these 3 tables not need accessed other module one.
the main reason use service manager or dependency injection container not other services access it. main reason apply dependency injection , thereby apply inversion of control. ease usage of objects, allow swap implementations easily, enhance ability test class , keep class responsible contained logic , not bothering dependencies.
in both examples, use service manager it's still same pattern: class (inside gettablegaclemployeepermission()
) decides get. either via service manager or via direct instantiation, not matter much.
by module configuration cannot grasp layers have in application , (esp. what's abstract class), in general, should inject it's dependencies:
abstract class someabstract { protected $permission; public function __construct(permission $permission) { $this->permission = $permission; } } class permission { protected $master; protected $slave; public function __construct(tableadapter $master, tableadapter $slave = null) { $this->master = $master; if (null !== $slave) { $this->slave = $slave; } } }
then create service manager configuration inject dependencies services define:
'factories' => array( 'login\model\permission' => function ($sl) { $master = $sl->get('my\masteradapter'); $slave = null; if ($some_flag) { $slave = $sl->get('my\slaveadapter'); } return new login\model\permission($master, $slave); }, 'login\some\concreteimplementation' => function ($sl) { $permission = $sl->get('login\model\permission'); return new login\some\concreteimplementation($permission); } ),
then request login\some\concreteimplementation
, injection ($master
, $slave
, $permission
) done you, enabling benefits described above.
Comments
Post a Comment