存储库模式可以用于加载“部分实体”吗? [英] Can repository pattern be used for loading of "partial entities"

查看:78
本文介绍了存储库模式可以用于加载“部分实体”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地了解域驱动设计中的存储库模式。存储库模式实现的所有示例都仅处理实体。但是,如果我只需要检索实体的某些部分怎么办?例如,我拥有具有大量属性的客户实体。我可以在ClientRepository中定义这样的内容吗?

I'm trying to get better understanding of repository pattern in Domain Driven Design. All examples of repository pattern implementation are dealing with entities only. But what if i need to retrieve only some part of entity? For example, i have Client entity with large amount of properties. Can i define something like this in ClientRepository:

public IEnumerable<string> GetClientsNames() {  ...  }

甚至:

class ClientBaseInfo       //includes some subset of properties of Client entity
{
    public string Name {get; set;}
    public string Surname {get; set;}
    public int Age {get; set;}
    public string Email {get; set;}
}

....

public IEnumerable<ClientBaseInfo> GetClientsBaseInfo() {  ...  }

这种实现的原因是性能。无论如何,我认为我的代码将被这种部分实体污染。在现实生活项目中是否以某种方式使用了这种方法?还是避免加载重实体的唯一方法是拆分表及其对应的实体或其他东西?

The reason for such implementation is performance. Anyway i consider my code will become polluted with this kind of "partial entities". Is this approach used in some way in real-life projects? Or the only way to avoid loading heavy entities is splitting of table and its corresponding entity or something else?

EDIT :是的,我是谈论诸如DTO之类的东西。我怀疑存储库是否打算处理此类对象,还是仅用于业务实体。我可以为特定情况定义很多不同的DTO,但是我的代码会变得太复杂吗?我没有答案,因为我没有足够的经验。我想知道一些经验丰富的人的意见。

EDIT: Yes, i'm talking about something like DTO. I doubt if repository is intended to deal with this kind of objects, or it is for business entities only. I can define a lot of different DTOs for specific situations, but can my code become too complicated? I have no answer, because i don't have enought experience. I would like to know opinion of somebody experienced.

推荐答案

对于这类查询,建议的方法是使用读取模型(

For these kinds of queries the suggested approach is to use a read model (CQRS-style).

因此,您可以实现一个非常薄的查询层,该查询层返回的原始结构将满足您的目的。在c#世界中,我选择从 DataRow DataTable 到DTO的任何东西(对于更复杂的结构)。

So you could implement a very thin query layer that returns as primitive a structure as would serve your purpose. In the c# world I opt for anything from a DataRow to a DataTable to a DTO (for more complex structures).

请记住,读取模型并不意味着最终的一致性,并且您的查询方可以处于任何级别,从同一表/数据库中的100%一致性到另一个数据库中的最终一致性

Remember that a read model does not imply eventual consistency and that your query side can be at any level from 100% consistency in same table / database to eventual consistency in another database.

因此,这类查询与存储库模式并不自然匹配。

So these kinds of queries do not have a natural fit to the repository pattern.

这篇关于存储库模式可以用于加载“部分实体”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆