扩展 WCF 数据服务以根据请求合成缺失数据
我有一个基于 LINQ to SQL 数据提供程序的 WCF 数据服务。
我正在查询“获取两个日期之间的所有记录”。
问题是我想合成两个额外的记录,以便我总是获得开始日期和结束日期的记录,以及来自数据库的所有记录。
有没有办法“拦截”请求,以便我可以合成这些记录并将其返回给客户端?
谢谢
I have got a WCF Data Service based on a LINQ to SQL data provider.
I am making a query "get me all the records between two dates".
The problem is that I want to synthesize two extra records such that I always get records that fall on the start and end dates, plus all the ones in between which come from the database.
Is there a way to "intercept" the request so I can synthesize these records and return them to the client?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑答案涉及使用“拦截器”。
刚刚偶然发现了这个...
http://msdn.microsoft.com/en-us/library/dd744842。 ASPX
I suspect the answer involves using "Interceptors".
Just stumbled across this...
http://msdn.microsoft.com/en-us/library/dd744842.aspx
我越想这个,我就越会说“请不要这样做”。问题在于,在 WCF 数据服务(或 OData)中,您返回的每个实体(实体 == 记录)都需要有其唯一的 URI。客户端还假设如果实体从服务器返回(除非它被删除),则可以再次访问该实体。
但在您的情况下,边界实体是由查询定义的,它们实际上只存在于查询的上下文中。给出不同的查询,它们是不同的。总而言之,它们的行为不像实体,它们的行为更像某种查询元数据。
不管怎样,如果你真的认为这是正确的事情……那么做起来是相当困难的。我能想到的唯一方法是挂钩从实体集返回的 IQueryable(将您自己的 IQueryable 放在 LINQ to SQL 的 IQueryable 之上)。然后,当执行查询时,您解析表达式树并找到定义范围的条件,然后返回 IEnumerable 的自定义实现,它将“合成”开头和结尾的两个特殊实体,并将返回其余部分来自底层 LINQ to SQL 结果。所有这些都是大量代码,而且绝对不容易做到。
第二种可能的方法是将其实现为服务操作(但要求客户端知道服务器上有一个特殊的操作来执行此操作)。这也更有意义,因为服务操作会将范围作为其参数而不是作为过滤器,因此您更容易找出范围(无需表达式树解析)。
The more I think about this the more I would say "please don't do this". The problem is that in WCF Data Services (or OData for that matter), each entity you return (entity == record) needs to have its unique URI. The clients also assume that if the entity is returned from the server (unless it was deleted), the entity can be accessed again.
In your case though, the boundary entities are defined by the query and they really only exist in the context of the query. Given a different query they are different. So all in all, they do not behave like entities, they behave more like some kind of query metadata.
Anyway, if you really think this is the right thing to do... It's rather hard to do this. The only approach I can think of is to hook into the IQueryable returned from the entity set (layer your own IQueryable on top of the one from LINQ to SQL). Then when a query gets executed, you parse the expression tree and find the conditions which define the range, then you return a custom implementation of IEnumerable which will "synthesize" the two special entities at the begining and at the end and it will return the rest from the underlying LINQ to SQL results. All of this is lot of code and it's definitely not easy to do.
A second possible way would be to implement this as a Service operation (requires that the client knows that there's a special operation on the server to do this though). It would also make a bit more sense, as the service operation would get the range as its parameters instead of as a filter and thus is much easier for you to figure out the range (no expression tree parsing).