什么类型的信息库查询层的多个实体的时候回来? [英] What type to return when querying multiple entities in Repository layer?

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

问题描述

我参与了这个问题,以下层:

I have the following layers involved in this question:


  • 服务层(使用IoC容器调用库)

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

  • 库层(EF的.edmx实施库)

很多时候,它真的直截了当:库层通过实体框架和回报的IList<查询数据库; SomeDomainEntity> 来调用者这是服务层。在键入的返回是在域模型中定义的类型。

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.

我遇到的问题是,当我需要跨越一个波苏斯查询,要返回从所有B和C,并获得数据。由于我没有在资源库中处理任何逻辑,我要回这些信息返回给处理方式(直接或更有可能通过调用域模型一些逻辑)服务层。不过,我没有的单一类型的再从资料库查询返回给调用者的结果。

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. 创建的域模型一个新的域实体,基本上是一个组成所有的数据我问,所以这个新的单一类型都可以返回。这似乎是错误的创建abritrary类型satisy查询的需求。

  2. 请服务层分别致电A,B,C实体的个人资料库,然后处理来自每个返回的对象中的数据。这似乎是一个很多额外的工作。

  3. 创建一个视图模型返回。这似乎是不合时宜的,对我来说也是如此。我利用视图模型类巨资我的业务层和UI层之间,但从来没有见过他们的杠杆从资源库中的回报。

我可以' T为唯一的一个在多个实体的查询,以获得需要被加入到式并返回给呼叫者数据背部的聚集。什么是常见的做法还是要对这个解决我的问题标准的方式?

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?

谢谢!

推荐答案

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

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)

我不会与你的第三项建议去(创建存储库中的ViewModels),因为它打破了分离

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

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

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