Dependency injection parent::__construct() antipattern?

Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
Stic
Člen | 28
+
0
-

Chcem sa spytat ci je moj postup spravny z hladiska dependency injection.

Pouzivam Nette @dev a v presenteroch injektujem sluzby pomocou anotacie @inject. Kedze som sa docital na fore ze pouzivat @inject by sa nemal nikde inde ako v presenteroch, tak sa snazim v modeli (a komponentach – ako kedy :) ) predavat zavislosti iba cez __constructor.

Dajme tomu ze mam BaseRepository, ktore potrebuje pracovat s DB:

<?php
namespace Model\Repositories;

use Nette;

/**
 * Basic operation on database tables
 */

 abstract class BaseRepository extends Nette\Object
 {
 	/** @var Nette\Database\Connection */
 	protected $connection;

 	public function __construct(Nette\Database\Connection $db)
 	{
 		$this->connection = $db;
 	}

 	/**
	 * Return object representing database table
	 * @return Nette\Database\Table\Selection
	 */
	protected function getTable()
	{
		return $this->connection->getSelectionFactory()->table($this->getTableName());
	}
 }
?>

Dalej mam Users_detailsRepository, ktora okrem toho este potrebuje servis FileManager:

<?php
namespace Model\Repositories;

use Nette\Database\Connection,
	Nette\Database\Table\ActiveRow;

class Users_detailsRepository extends BaseRepository
{

	/** @var Utilities\FileManager */
	private $fileManager;

	public function __construct(\Utilities\FileManager $fileManager)
	{
		$this->fileManager = $fileManager;
	}
}
?>

Samozrejme, ze tak stratim $db->connection z BaseRepository. Preto sa chcem spytat ci to nie je nejaka antipattern, ked spravim toto:

<?php
namespace Model\Repositories;

use Nette\Database\Connection,
	Nette\Database\Table\ActiveRow;

class Users_detailsRepository extends BaseRepository
{

	/** @var Utilities\FileManager */
	private $fileManager;

	public function __construct(Connection $db, \Utilities\FileManager $fileManager)
	{
		parent::__construct($db);
		$this->fileManager = $fileManager;
	}
}
?>

Pripadne, ci existuje nejaky elegantnejsi sposob?

Dakujem

Editoval Stic (10. 10. 2013 11:49)

mkoubik
Člen | 728
+
0
-

To je naprosto v pořádku.