我正在使用類似SOA的應用程序的依賴注入。你如何使用依賴性服務和依賴注入?
例如,我有一個VoteService,用於爲投票和評論投票。
我已經有4個依賴關係,文章,評論,數據庫抽象層和需要投票的用戶。
所以我的構造函數有4個參數來填充我的對象。
我總是聽說超過2/3的參數是錯誤代碼設計的警告,我可能會同意這一點。
也許我的VoteService沒有很好的設計。
我可能必須在第&評論服務中移動投票投票嗎?
您認爲如何?
class ArticleService {
public function createArticle(); // and other crud methods
}
class VoteService {
public function __construct($entityManager, $articleService, $commentService, $configurationService);
// here is the constructor with much arguments
public function addArticleVote()
{
$vote = new ArticleVote();
$vote->setType(ArticleVote::TYPE_UP)
$vote->setArticle($article);
$this->entityManager->persist($vote);
$this->entityManager->flush();
}
// the same method applies for comment
}
3是教條小數目。就我個人而言,我開始認爲,也許這應該在6-7左右之後完成。但無論如何,因爲你不會編寫調用這個構造函數的代碼,所以這個論點是沒有意義的。 – Jon
爲了解決這個問題,並幫助關閉我腦海中尖叫的聲音,我通過在數組中放置15個參數來作弊。然後,我只通過一個變量right:D –