【发布时间】:2012-02-15 10:26:47
【问题描述】:
类似下面的构造可能吗?
public IQueryable<T> AllWithFetch<TRelated>(IQueryable<T> existing, params Expression<Func<T, TRelated>>[] fetchExpressions)
{
return fetchExpressions.Aggregate(existing, (current, exp) => current.Fetch(exp));
}
然后可以这样调用...
var allDetails = this.preGrantDetailRepository
.AllWithFetch(this.preGrantDetailRepository.All, x => x.Case, x => x.CaseOwner)
基本上,我试图将 NHibernate 的获取策略添加到我们的抽象存储库中,以使我们能够在不破坏存储库模式的情况下从逻辑层指定这些策略。例如,如果我们从 NHibernate 更改为另一个 ORM,我们可以提供相同的存储库方法,但为该 ORM 实现。
当我尝试在 param 数组中链接多个 func 时出现问题。
所以这行得通...
var allDetails = this.preGrantDetailRepository
.AllWithFetch(this.preGrantDetailRepository.All, x => x.Case)
但这失败了,“类型参数不能从它们的用法中推断出来”消息
var allDetails = this.preGrantDetailRepository
.AllWithFetch(this.preGrantDetailRepository.All, x => x.Case, x => x.CaseOwner)
我正在使用 .NET 3.5、存储库模式、Fluent NHibernate、SQL Server 2008
编辑
我在下面的 Porges 答案的帮助下解决了这个问题,所以我接受了它。问题确实来自对 TRelated 的错误使用。这是存储库中的工作方法...
public IQueryable<T> AllWithFetch<T>(IQueryable<T> existing, params Expression<Func<T, Entity>>[] fetchExpressions)
{
return fetchExpressions.Aggregate(existing, (current, exp) => current.Fetch(exp));
}
现在 AllWithFetch 不是 TRelated,我在 Func 中使用两个实体(Case 和 CaseOwner)的超类。
感谢大家的帮助
【问题讨论】:
-
.Case和.CaseOwner的返回类型是什么?
标签: c# fluent-nhibernate repository-pattern