使用 Linq-to-SQL、IOC 和 Repository 模式预取数据 [英] Prefetching data in with Linq-to-SQL, IOC and Repository pattern

查看:21
本文介绍了使用 Linq-to-SQL、IOC 和 Repository 模式预取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Linq-to-SQL 我想预取一些数据.

using Linq-to-SQL I'd like to prefetch some data.

1) 常见的解决方案是处理 DataLoadOptions,但在我的架构中它不起作用,因为:

1) the common solution is to deal with DataLoadOptions, but in my architecture it won't work because :

  • 必须在第一次查询之前设置选项
  • 我正在使用 IOC,所以我没有直接实例化 DataContext(我无法在实例化时执行代码)
  • 我的 DataContext 在网络请求期间是持久的

2) 我看到了另一种可能性,基于在方法中加载数据及其子项,然后仅返回数据(因此子项已加载)在这里查看示例

2) I have seen another possibility based on loading the data and its childs in a method, then returning only the data (so the child is already loaded) see an example here

尽管如此,在我的架构中,它不能不工作:

Nonetheless, in my architecture, it cannot not work :

  • 我的查询级联出我的存储库,可以被许多将添加子句的服务使用
  • 我使用接口,linq-to-sql 对象的具体实例不会离开存储库(是的,您可以使用接口和添加子句)
  • 我的存储库是通用的

是的,这个架构很复杂,但它非常酷,因为我可以玩乐高之类的代码;)

Yes, this architecture is quiet complicated, but it's very cool as I can play with the code like lego ;)

我的问题是:预取数据的其他可能性是什么?

My question is : what are the other possibilities to prefetch a data ?

推荐答案

在我的应用程序中,我可能使用了您的潜在解决方案 #2 的变体.这有点难以解释,但很简单:我使用自定义 懒惰类,以便从我利用 IQueryable 所利用的特定于 LinqToSql 的不同执行中抽象出来.好处:

In my app i use perhaps a variation to your potential solution #2. It's somewhat difficult to explain but simply: i chain and defer lazy loading in my model with custom lazy classes so as to abstract away from the LinqToSql-specific Differed Execution that i take advantage of with IQueryable. Benefits:

  • 我向上的领域模型和服务层不一定依赖于 LinqToSql 提供程序(如果我愿意,我可以用接口替换我的 DAL)
  • 我的服务方法可以并且确实返回带有多个锚点"的完整对象图,用于使用抽象出特定延迟加载实现的类进行延迟加载 - 因此我可以使用特定于 LinqToSql 的不同执行或其他东西(例如匿名委托. 再次参考这个答案)
  • 我可以在整个应用程序中维护 IQueryable 结果(如果我愿意,甚至可以维护 UI),从而允许无限的 LINQ 查询链接,而不必担心性能.
  • My Domain Model and Service layer upwards does not necessarily have to depend on the LinqToSql provider (i can swap out my DAL with interfaces if i want to)
  • My Service methods can and do return complete object graphs with multiple 'anchor points' for lazy loading using classes that abstract away a particular lazy loading implementation - so i can use LinqToSql-specific Differed Execution or something else (eg. anon delegates. again, refer to this answer)
  • I can maintain IQueryable results throughout my app (even to the UI if i want to) thus allowing infinite LINQ query chaining without having to worry about performance.

这篇关于使用 Linq-to-SQL、IOC 和 Repository 模式预取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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