I'm trying to understand the principles of the Repository question. But i've stumbled upon another question. To me it looks like the repository pattern causes alot of overhead.
An example:
My Entity class looks like this:
class newsEntity
{
private $title;
private $content;
private dateCreated;
private $author;
private $category; // just 1 category possible for simplicity
# .. Getter and Setter methods ...
}
The repository looks like this (simplified ofcourse)
class newsRepository
{
public function getNewsByYear ( $year )
{
$newsList = array();
// Some ORM code which fills $newsList with newsEntity objects
}
}
The client code looks something like this:
$repo = new newsRepository();
$news = $repo->getNewsByYear(2011);
foreach ( $news as $item )
echo $item->getTitle() . " " . $item->getDateCreated();
This simply shows a list of all newsitems it can find from the year 2011 with the title and creation date.
Now the problem that i have is, that i'm only using the $title and $dateCreated property of the Entity object. But all other properties are filled too, but i never use them! So that means that if getNewsByYear retrieves 3000 records, that it also fills alot of properties in an object which never gets used... Shouldn't that be a big problem...? This ofcourse gets even worse when its an Aggregate...
Another thing which i'm not sure about is; Does my repository ALWAYS need to return an Entity object? Or can it also return just a string when i just need to display the title or anything like that..???
I don't think there's a problem with your repository returning just primitives or stripped down DTOs for simple queries. Inside the repository you can use your ORM to just load the fields you need. You don't have to preload the whole objects.
class newsOverviewDto
{
private $title;
private $dateCreated;
// + Getters/Setters
}
class newsRepository
{
public function getNewsByYearForOverview ( $year )
{
$newsList = array();
// Some ORM code which fills $newsList with
// DTOs that contain title and date
}
}
// Usage:
$repo = new newsRepository();
$newsOverviewDtos = $repo->getNewsByYearForOverview(2011);
foreach ( $newsOverviewDtos as $item )
echo $item->getTitle() . " " . $item->getDateCreated();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With