通过 WCF 服务公开 IQueryable
我一直在学习 IQueryable 和查询的延迟加载/延迟执行。
是否可以通过 WCF 公开此功能?我想公开一个返回 IQueryable 的 LINQ-to-SQL 服务,然后我可以在客户端上执行其他查询,最后使用 .ToList() 执行。 OData 格式是否适用于这种情况?
如果可能的话,这种技术的术语是什么?我可以遵循哪些好的教程?谢谢。
I've been learning about IQueryable and lazy loading/deferred execution of queries.
Is it possible to expose this functionality over WCF? I'd like to expose a LINQ-to-SQL service that returns an IQueryable which I can then perform additional queries on at the client, and finally execute using a .ToList(). Is OData format applicable at all in this context?
If possible, what is the term for this technique and what are some good tutorials I can follow? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该检查 WCF 数据服务,它允许您定义 Linq 查询客户。 WCF 数据服务可能是满足您需求的唯一解决方案。
IQueryable 仍然只是一个接口,其功能取决于实现该接口的类型。您不能直接公开 Linq-To-Sql 或 Linq-To-Entities 查询。有多种原因,例如短暂的上下文或序列化将执行查询,因此客户端将获得所有对象的列表而不是查询。
You should check WCF Data Services which will allow you to define Linq query on the client. WCF Data Services are probably the only solution for your requirement.
IQueryable is still only interface and the functionality depends on the type implementing the interface. You can't directly expose Linq-To-Sql or Linq-To-Entities queries. There are multiple reasons like short living contexts or serialization which will execute the query so the client will get list of all objects instead of the query.
据我所知,数据上下文不可序列化,这意味着您无法使用 WCF 传递它
as far as i know, the datacontext is not serializable meaning that you cannot pass it around with WCF
您可以使用 http://interlinq.codeplex.com/,它允许您通过 WCF 发送 Linq 查询。
WCF 数据服务只能与 webHttpBinding 一起使用,并且不能表达所有 Linq 查询。使用 WCF 数据服务时编写查询并不那么有吸引力 - 需要字符串表达式,例如:
You can use http://interlinq.codeplex.com/ which allows you to send Linq query over WCF.
WCF Data Services only can be using with webHttpBinding and not all Linq queries can be expressed. Writing queries when WCF Data Service is used is not so attractive - requires string expressions such as:
https://remotelinq.codeplex.com/ 是另一种选择。但它可以在 AppDomain 中扫描当前程序集并序列化它们。该技术不适合 WinRT,因为 WinRT 应用程序没有域
https://remotelinq.codeplex.com/ is another choice. But it works in AppDomain to scan the Current Assemblies and serialize them. This tech is not suitable to WinRT as no Domains for WinRT App
我一直在努力解决同样的问题,并意识到一个结构良好的问题就可以解决问题。
IQueryable 基本上用于在将查询发送到数据库调用之前过滤查询,因此您不是获取 1000 条记录并仅过滤 10 条,而是首先获取这 10 条记录。该过滤属于您的服务层,但如果您正在构建 API,我会假设您将使用 URL 中的 AND/OR 参数来映射它。
http://{主机}/{实体}/q?name=john&age=21。
所以你最终会得到这样的结果:
你可以找到一个非常好的示例 [此处]
最后,由于来自 WCF 的负载很可能是 JSON,因此您可以(并且应该)在集合内的域模型中反序列化它们。直到此时应该发生分页,因此我建议使用一些 WCF 缓存(并且由于它是 HTTP,所以它非常简单)。您仍然会在 WebApp 端使用 LINQ,只是没有“WHERE”LINQ 子句(除非您想动态创建上面表达的 URL?)
对于复杂的 OR 查询,您介意最终会出现多个 WCF 查询(每个 WCF 查询 1 个)。 “AND”),然后将它们连接在一起
I've been struggling with the same question, and realized that a well formed question is a problem solved.
IQueryable basically serves to filter the query before sending it to your DB call so instead of getting 1000 records and filter only 10, you get those 10 to begin with. That filtering belongs to your Service Layer, but if you are building an API I would assume you would map it with AND/OR parameters in your URL.
http://{host}/{entity}/q?name=john&age=21.
So you end up with something like this:
You can find a very good sample [here]
Lastly, since your payload from the WCF will be most likely a JSON, you can (and should) then deserialize them in your Domain Models inside a collection. It is until this point where the paging should occur, so I would recommend some WCF caching (and since its HTTP, it's really simple). You still will be using LINQ on the WebApp side, just w/o "WHERE" LINQ clause (unless you want to dynamically create the URL expressed above?)
For a complex OR query, you mind end up with multiple WCF queries (1 per "AND") and then concatenate them all together
如果可以发送 IQuerable<>在 WCF 上,这在安全方面并不是一件好事,因为 IQuerable<>可以将连接字符串之类的东西暴露给数据库。
不过,之前的一些评论似乎很有希望。
If it's possible to send IQuerable<> over WCF it's not a good thing security wise, since IQuerable<> could expose stuff like the connection string to the database.
Some of the previous comments seem promising though.