要返回的IQueryable< T>或者没有返回的IQueryable< T> [英] To return IQueryable<T> or not return IQueryable<T>

查看:163
本文介绍了要返回的IQueryable< T>或者没有返回的IQueryable< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个封装我的LINQ to SQL数据上下文的存储库类。该库类是包含所有数据层逻辑(和缓存等)等业务线的类。

I have a repository class that wraps my LINQ to SQL Data Context. The repository class is a business line class that contains all the data tier logic (and caching and such).

下面是我的我的回购接口V1。

Here's my v1 of my repo interface.

public interface ILocationRepository
{
    IList<Location> FindAll();
    IList<Location> FindForState(State state);
    IList<Location> FindForPostCode(string postCode);
}

不过来处理分页的FindAll,我辩论是否要暴露的IQueryable&LT; ILocation&GT;代替的IList简化为的情况,例如寻呼接口

But to handle paging for FindAll, I'm debating whether or not to expose IQueryable<ILocation> instead of IList to simplify the interface for circumstances such as paging.

有哪些优点和缺点,从数据回购暴露IQueryable的?

What are the pros and cons to exposing IQueryable from the data repo?

任何帮助是非常AP preciated。

Any help is very much appreciated.

推荐答案

的优点;组合性:

  • 呼叫者可以添加过滤器
  • 呼叫者可以添加分页
  • 呼叫者可以添加排序

的缺点;非可测性:

  • 您的资料库已不再正确单元测试;你不能依靠一个:它的工作,B:什么它;
    • 呼叫者可以添加一个不可翻译的功能(即没有TSQL映射;在运行时休息)
    • 呼叫者可以添加一个过滤器/排序,使得它像狗一样执行
    • Your repository is no longer properly unit testable; you can't rely on a: it working, b: what it does;
      • the caller could add a non-translatable function (i.e. no TSQL mapping; breaks at runtime)
      • the caller could add a filter/sort that makes it perform like a dog

      有关稳定,我已经采取为不可以暴露的IQueryable&LT; T&GT; 防爆pression&LT; ...&GT; 在我的仓库。这意味着,我知道如何存储库的行为,我的上层可以使用模拟考试,无需担心与实际存储库支持呢? (强制集成测试)。

      For stability, I've taken to not exposing IQueryable<T> or Expression<...> on my repositories. This means I know how the repository behaves, and my upper layers can use mocks without worrying "does the actual repository support this?" (forcing integration tests).

      我仍然可以使用的IQueryable&LT; T&GT; 等的的资源库 - 但不超过边界。我发布了一些更多的心思在这里。这也很容易把页面参数库接口。您甚至可以使用扩展方法(界面)来添加的的页面参数,使混凝土类只有1个的方法来实现,但有可能是2或3重载提供给调用者。

      I still use IQueryable<T> etc inside the repository - but not over the boundary. I posted some more thoughts on this theme here. It is just as easy to put paging parameters on the repository interface. You can even use extension methods (on the interface) to add optional paging parameters, so that the concrete classes only have 1 method to implement, but there may be 2 or 3 overloads available to the caller.

      这篇关于要返回的IQueryable&LT; T&GT;或者没有返回的IQueryable&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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