存储库是否应该向服务提供所有数据?

发布于 2024-10-21 07:29:58 字数 657 浏览 6 评论 0原文

我的意思是,假设我的存储库中有一个方法:

Public Function GetCustomerByState(ByVal State As String) As IQueryable(Of Customer)

服务是否能够以此扩展的形式获取附加数据,以获取客户的订单:

<Extension()> _
    Public Function Include(Of T)(ByVal Source As IQueryable(Of T), ByVal Path As String) As IQueryable(Of T)
        Dim ObjectQuery = CType(Source, ObjectQuery(Of T))
        If (ObjectQuery IsNot Nothing) Then
            Return ObjectQuery.Include(Path)
        End If
        Return Source

    End Function

此扩展最有可能与通用方法一起使用存储库。

或者,您是否应该对每个聚合根的存储库进行具体实现,以准确返回服务所需的内容,而没有更多或更少?

那么,您的存储库是否应该返回 IQueryable?

What I mean by that is, say I have a method in my repository:

Public Function GetCustomerByState(ByVal State As String) As IQueryable(Of Customer)

Should the service be able to get additional data in the form of say this extension to say get the orders for the customer:

<Extension()> _
    Public Function Include(Of T)(ByVal Source As IQueryable(Of T), ByVal Path As String) As IQueryable(Of T)
        Dim ObjectQuery = CType(Source, ObjectQuery(Of T))
        If (ObjectQuery IsNot Nothing) Then
            Return ObjectQuery.Include(Path)
        End If
        Return Source

    End Function

This extension is most likely used with a generic repository.

Or should you have concrete implementations of the repositories per aggregate root that return to the service exactly what it needs and nothing more or less?

Then, should your repositories even return IQueryable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛歌少女 2024-10-28 07:29:58

我只会提供有限的方法集,例如:

IQueryable<T> GetQuery();
T GetByKey(K key);

原因是存储库用于提供对聚合根的数据访问的抽象,但业务逻辑负责定义应检索哪些数据。如果将太多逻辑传递到存储库中,您将在服务和存储库之间划分逻辑。此外,服务中的大多数数据检索方法不会执行任何操作 - 它们只会调用存储库方法。

Martin Fowler 清楚地说明了这一点:

存储库在
域和数据映射层,作用
就像内存中的域对象
收藏。客户端对象构造
以声明方式查询规范和
将它们提交到存储库
满意。

在过去的几年里,Repository 和 UnitOfWork 模式在 .NET 世界中变得非常流行。但没有人谈论这个家族的第三种模式 - 规范。规范是传递到存储库的抽象查询,用于定义应返回哪些数据。 .NET 世界没有明确使用此模式,因为 IMO Linq-To-Entities 查询是规范。

I would offer only limited set of methods like:

IQueryable<T> GetQuery();
T GetByKey(K key);

The reason is that repository is used to provide abstraction of data access to agregation roots but the business logic is responsible for defining what data should be retrieved. If you pass too much logic into repository you will divide the logic between service and repository. Moreover most of your data retrieval methods in service will do nothing - they will just call repository method.

Martin Fowler says this clearly:

A Repository mediates between the
domain and data mapping layers, acting
like an in-memory domain object
collection. Client objects construct
query specifications declaratively and
submit them to Repository for
satisfaction.

In past years Repository and UnitOfWork patterns became very popular in .NET world. But nobody speaks about third pattern from this family - Specification. Specification is an abtracted query passed to repository to define what data should be returned. .NET world doesn't use this pattern explicitly because IMO Linq-To-Entities queries are specifications.

苯莒 2024-10-28 07:29:58

我将从存储库中返回服务所需的所有内容。这是一个很好的做法,可以防止您编写重复的查询。我在 Repository with EF Code First 上写了一个答案,可能会有所帮助: Entity Framework 4 CTP 4 / CTP 5 通用存储库模式和可单元测试

请注意 - 存储库模式有许多不同的实现,并找到一个解决方案“感觉“正确”是我推荐的。在小型项目中,您可以返回 IQueryable,但如果它增长,您可能会后悔这个设计决策,因为很难保持事物干燥。

I would return everything the service needs from the Repository. It is good practice and will keep you from potentially writing duplicate queries. I wrote a SO answer on Repository with EF Code First that may help : Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable

Just a note - There are MANY different implementations of the Repository pattern and finding a solution that "feels" right is what I would recommend. In small projects you could return IQueryable but if it grows, you may regret the design decision as it becomes difficult to keep things DRY.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文