Can repository pattern be used for loading of “partial entities”

倖福魔咒の 提交于 2019-12-06 06:13:44

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

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).

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.

hgulyan

It always depends, as usual.

Your Business Logic should not be dependent on database structure.

It should represent your business logic. That means, that you need to be careful when you design your entities and their relations.

On the other hand it depends why do you need to load only a part of entity's properties.

Maybe you should change domain logic, if you have so many properties in a class?

Maybe you need Data Transfer Objects?

You can create DTO class, which would be a "simple version" of your entity just to transfer data?

UPDATE:

You can use dto's in a repository. There're some cases, that it's need (reports, etc. ).

Here're some examples on that for NHibernate

NHibernate QueryOver projections - projecting collections to DTO

Fill property of DTO with SubQuery in NHibernate Query

This approach is acceptable for a few cases.

If you need to use dto's all over your application, it means you need to change your domain layer.

Maybe you should divide your classes into smaller ones and load them separately.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!