我有一个带有返回 IQueryable 的查询方法的通用存储库。在我的调用代码中,我可以做这样的事情
_repository.Query<MyClass>(x=>x.EntityId == 1).Fetch(x=>x.MyClassChild).ToList()
但是,我将无法测试调用代码(据我所知)。所以我正在尝试执行以下操作
public class Repository : IRepository
{
....
public FetchedResult<TQueried, TRelated> ThenFetch<TQueried, TFetch, TRelated>(INhFetchRequest<TQueried, TFetch> query, Expression<Func<TFetch, TRelated>> relatedObjectSelector)
{
INhFetchRequest<TQueried, TRelated> nhFetchRequest = query.ThenFetch(relatedObjectSelector);
return new FetchedResult<TQueried, TRelated>(this, nhFetchRequest);
}
public FetchedResult<TOriginating, TRelated> Fetch<TOriginating, TRelated>(IQueryable<TOriginating> query, Expression<Func<TOriginating, TRelated>> relatedObjectSelector)
{
INhFetchRequest<TOriginating, TRelated> nhFetchRequest = query.Fetch(relatedObjectSelector);
return new FetchedResult<TOriginating, TRelated>(this, nhFetchRequest);
}
}
--
public class FetchedResult<TQueried, TRelated>
{
private readonly IRepository _repository;
private readonly INhFetchRequest<TQueried, TRelated> _query;
public FetchedResult(IRepository repository, INhFetchRequest<TQueried, TRelated> query)
{
_repository = repository;
_query = query;
}
public FetchedResult<TQueried, TRelated> ThenFetch<TFetch>(Expression<Func<TFetch,TRelated>> relatedObjectSelector)
{
return _repository.ThenFetch(_query, relatedObjectSelector);
}
}
因此,第一次调用 Fetch 有效,但调用 repositor.ThenFetch 接受 INhFetchRequest 查询,但返回 INhFetchRequest。所以我不能再使用 FetchedResult 再次调用 ThenFetch 。
我认为这是问题所在。在这一点上,我的大脑非常混乱。如果有人可以帮助让我知道,我可以尝试提供更多或更好的信息。
现在我知道我可以使用静态来做到这一点,但是我的目标是能够模拟对 Fetch 的调用。
谢谢,
拉伊夫