存储库模式和开销..?
我试图理解存储库问题的原理。但我偶然发现了另一个问题。在我看来,存储库模式会导致大量开销。
一个例子:
我的实体类看起来像这样:
class newsEntity
{
private $title;
private $content;
private dateCreated;
private $author;
private $category; // just 1 category possible for simplicity
# .. Getter and Setter methods ...
}
存储库看起来像这样(当然是简化的)
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 年找到的所有新闻站点的列表,以及标题和创建日期。
现在我遇到的问题是,我只使用实体对象的 $title 和 $dateCreated 属性。但所有其他属性也已填充,但我从不使用它们!因此,这意味着如果 getNewsByYear 检索 3000 条记录,它还会填充一个从未使用过的对象中的大量属性...这不是一个大问题吗...?当它是一个聚合时,这当然会变得更糟......
我不确定的另一件事是;我的存储库是否始终需要返回实体对象?或者当我只需要显示标题或类似内容时它也可以只返回一个字符串..???
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..???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的存储库仅返回原语或针对简单查询简化 DTO 没有问题。在存储库中,您可以使用 ORM 来加载您需要的字段。您不必预加载整个对象。
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.
您可以使用临时DTO(数据传输对象)。它是对象的简化版本,仅包含查询所需的成员。
例如,您不使用
Entity
及其所有成员,而是使用仅包含dateCreated
的EntitySummary
。当然,这意味着您必须更改接口和服务层才能使用此 DTO 而不是原始对象。You can use a temporary DTO (Data Transfer Object). It's a cut-down version of your object that only has the members you need for your query(ies).
For exemple, instead of using
Entity
with all its members, you useEntitySummary
which only has anddateCreated
. Of course, that means you'll have to change your interfaces and your service layer to use this DTO instead of the original object.