查询存储库层中的多个实体时返回什么类型? [英] What type to return when querying multiple entities in Repository layer?

查看:161
本文介绍了查询存储库层中的多个实体时返回什么类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 服务层(使用IoC调用存储库)

  • 域模型(POCO /域实体,定义的存储库接口)

  • 资源库层(EF .edmx和已实现的存储库)



很多时候它真的很直接:存储库层通过实体框架查询数据库,并将 IList< SomeDomainEntity> 发送给调用者服务层返回的类型是域模型中定义的类型。



我遇到的问题是当我需要查询POCO A ,B和C,并从全部获取数据以返回。由于我没有处理存储库中的任何逻辑,所以我需要将这些数据返回到要处理的服务层(直接或更可能通过调用域模型上的一些逻辑)。但是,我从回收到调用者的存储库查询的结果中没有一个单一类型



匿名类型的在我看到的示例中处理这个,但是由于我没有直接在Repository中处理返回的数据的逻辑,需要返回,我需要一个物理类型才能返回。以下是我想到的一些解决方案,但不能肯定我喜欢任何一种:


  1. 在域模型中创建一个基本上是我查询的所有数据,所以这个新的单一类型可能会被返回。创建abritrary类型以满足查询需求似乎是错误的。

  2. 使服务层单独调用A,B,C实体上的各个存储库,然后处理每个返回对象的数据。这似乎是很多额外的工作。

  3. 创建一个ViewModel返回。这似乎对我来说是不合适的。我在我的服务层和UI层之间使用ViewModel类,但是从来没有看到他们从存储库返回。

t是唯一一个查询多个实体以获取需要添加到类型并返回给调用者的数据集合的集合。



谢谢!

常见的做法或标准的方法来解决我的问题解决方案

如果这些实体相关,并且您在一个地方查询所有这些实体,那么您应该尝试找到一个您的域模型中的聚合根,或者如果它不存在,您应该引入一个新的,如您在第一个选项中所述。这是没有错的,直到它是有道理的。它应该建模域概念,你可能有一个,因为你创建了这个存储库方法。



如果这些实体是不相关的在某些方面,但不是像上面那样紧密相关),您只需要一次性获取它们,那么您应该在可以使用多个存储库并组合结果对象的服务层中处理该问题。



你可能听说过导航属性渴望加载但我写在这里,因为它可能是你的问题的另一个答案(我看不到你的域模型)



我不会与你的第三个建议(在存储库中创建视图模型),因为它会中断分隔。


I have the following layers involved in this question:

  • Service Layer (Using IoC to call Repository)
  • Domain Model (POCO / Domain Entities, defined repository Interfaces)
  • Repository Layer (EF .edmx and implemented repositories)

A lot of times it's really straight forward: Repository layer queries database via Entity Framework and returns IList<SomeDomainEntity> to caller which was Service Layer. The type returned is a type defined in the Domain Model.

The problem I'm running into is when I need to query across POCOs A,B, and C and get data from all to be returned. Since I don't handle any logic in the repository I need to return this data back to the Service layer to be processed (either directly or more likely by calling some logic on domain model). However I don't have a single type anymore from the results of the repository query to return to the caller.

An anonymous type of course handles this in examples I see, but since I'm not processing that logic from returned data directly in the Repository and it needs to be returned, I need a physical type to return. Here are some solutions I thought of but not sure I like any:

  1. Create a new domain entity in Domain Model that essentially is a composition of all the data I queried, so this new single type may be returned. This seems wrong to create abritrary types to satisy query needs.
  2. Make the service layer call the individual repositories on A,B,C entities separately and then deal with the data from each returned object. This seems like a lot of extra work.
  3. Create a ViewModel to return. This seems out of place to me as well. I leverage ViewModel classes heavily between my service layer and UI layers, but never seen them leveraged for return from the repository.

I can't be the only one querying across multiple entities to get a conglomeration of data back that needs to be added to a type and returned to the caller. What is a common practice or standard way of going about this to solve my problem?

Thanks!

解决方案

If those entities are related and you query all of them in one place then you should try to find an aggregate root of them in your domain model or if it doesn't exist yet you should introduce a new one, as you said in the first option. This is not wrong until it makes sense. It should model a domain concept and you probably have one because you created that repository method.

If those entities are not related (well, probably related in some way, but not that tightly as above) and you just want to get them in one go then you should handle that in the service layer where you can use multiple repositories and compose a result object.

You probably heard of the concept of navigation properties and eager loading but I write it here because it may be another answer to your question (I don't see your domain model)

I wouldn't go with your third suggestion (create viewmodels in the repository) because it breaks the separations.

这篇关于查询存储库层中的多个实体时返回什么类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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