我想了解存儲庫問題的原理。但我偶然發現了另一個問題。對我來說,它看起來像存儲庫模式導致大量的開銷。存儲庫模式和開銷..?
一個例子:
我的實體類如下所示:
class newsEntity
{
private $title;
private $content;
private dateCreated;
private $author;
private $category; // just 1 category possible for simplicity
# .. Getter and Setter methods ...
}
的倉庫看起來像這樣(ofcourse簡體)
class newsRepository
{
public function getNewsByYear ($year)
{
$newsList = array();
// Some ORM code which fills $newsList with newsEntity objects
}
}
客戶端代碼看起來像這個:
$repo = new newsRepository();
$news = $repo->getNewsByYear(2011);
foreach ($news as $item)
echo $item->getTitle() . " " . $item->getDateCreated();
這只是顯示了它可以從2011年標題和創建日期找到的所有newsitems列表。
現在我的問題是,我只使用Entity對象的$ title和$ dateCreated屬性。但所有其他屬性也填充了,但我從不使用它們!所以這意味着如果getNewsByYear檢索到3000條記錄,那麼它也會填充一個永遠不會被使用的對象中的很多屬性......這不應該是一個大問題......?這當然會變得更糟,當它的一個總和...
另一件我不確定的事情是;我的存儲庫是否總是需要返回一個實體對象?或者它也可以只返回一個字符串,當我只需要顯示標題或類似的東西..?