且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

要返回的IQueryable< T>或者没有返回的IQueryable< T>

更新时间:2022-02-10 14:50:32

的优点;组合性:

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

的缺点;非可测性:

  • 您的资料库已不再正确单元测试;你不能依靠一个:它的工作,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< T> 防爆pression< ...> 在我的仓库。这意味着,我知道如何存储库的行为,我的上层可以使用模拟考试,无需担心与实际存储库支持呢? (强制集成测试)。

      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.