NHibernate Linq渴望使用通用存储库加载 [英] NHibernate Linq Eager Loading with generic repository

查看:365
本文介绍了NHibernate Linq渴望使用通用存储库加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们为所有实体使用通用存储库,该存储库公开了IQueryable(使用NH3 linq支持),然后由我们的服务层用于构造特定查询.

Currently we are using a generic repository for all our entities that exposes an IQueryable (using NH3 linq support) that is then used by our service layer to construct specific queries.

我现在需要热切地加载一个关联.有什么方法可以公开IQueryable并传递可选的访存表达式?我看到的问题是,提取必须在表达式中排在最后(按照 http://mikehadlow.blogspot.com/2010/08/nhibernate-linq-eager-fetching.html ).

I now need to eagerly load an association. Is there any way I can expose an IQueryable and pass in optional fetch expressions? The problem I see is that Fetch must come last in the expression (as per http://mikehadlow.blogspot.com/2010/08/nhibernate-linq-eager-fetching.html).

我很好奇其他人是如何做到这一点的.

I'm curious as to how others have achieved this.

我确实考虑过也许将Linq规范传递到存储库,以便可以在调用Fetch之前对其进行评估.但是,我仍然需要一些传递Fetch表达式的方法.

I did consider perhaps passing Linq specifications to the repository so that these could evaluated prior to calling Fetch. I would however still need some way of passing in the Fetch expressions.

谢谢 本

推荐答案

我使用FindOneFindAll存储库调用的重载来实现这一点.

I use overloads of my FindOne and FindAll repository calls to achieve this..something like:

Function FindOne(ByVal spec As ILinqSpecification(Of T)) As T
Function FindOne(ByVal spec As ILinqSpecification(Of T), ByVal strategy As IFetchingStrategy(Of T)) As T
Function FindAll(ByVal spec As ILinqSpecification(Of T)) As IQueryable(Of T)
Function FindAll(ByVal spec As ILinqSpecification(Of T), ByVal strategy As IFetchingStrategy(Of T)) As IQueryable(Of T)

等.

也许不是最干净的方法,但它确实可以做到.我不确定trunk linq提供程序是否仍然存在问题,但是我还可以根据是否获取策略来决定是否在FindAll方案中对我的结果应用独特的结果转换器包含一个集合.

Perhaps not the cleanest approach, but it does the job. I'm not certain if this is still an issue with the trunk linq provider or not, but I can also decide whether or not to apply the distinct result transformer to my results in the FindAll scenarios based upon whether or not my fetching strategy contains a collection.

我的规范和获取策略的实现是基于 ncommon 项目中可用的.

My specification and fetching strategy implementations are based upon those available in the ncommon project.

作为参考,我完整的通用只读"存储库界面如下:

For reference, my full generic "read-only" repository interface is as follows:

Public Interface IReadOnlyRepositoryWithTypedId(Of T As IEntityWithTypedId(Of IdT), IdT)

    Function LoadById(ByVal id As IdT) As T
    Function GetById(ByVal id As IdT) As T
    Function FindOne(ByVal spec As ILinqSpecification(Of T)) As T
    Function FindOne(ByVal spec As ILinqSpecification(Of T), ByVal strategy As IFetchingStrategy(Of T)) As T
    Function GetCount() As Integer
    Function GetCount(ByVal spec As ILinqSpecification(Of T)) As Integer
    Function HasAny(ByVal spec As ILinqSpecification(Of T)) As Boolean
    Function FindAll(ByVal spec As ILinqSpecification(Of T)) As IQueryable(Of T)
    Function FindAll(ByVal spec As ILinqSpecification(Of T), ByVal strategy As IFetchingStrategy(Of T)) As IQueryable(Of T)
    Function FindAll() As IQueryable(Of T)
    Function FindAll(ByVal strategy As IFetchingStrategy(Of T)) As IQueryable(Of T)

End Interface

这篇关于NHibernate Linq渴望使用通用存储库加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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