DataService 是否实现了 IDisposable?
以下是一个示例服务:
public class MyWcfDataService : DataService<MyEFModel>
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public IQueryable<GetMyListEF> GetMyList()
{
using (MyEfModel context = this.CurrentDataSource)
{
return context.GetMyListEF().ToList().AsQueryable();
}
}
}
我应该使用 using
语句吗?这有点让 IQueryable
毫无意义,因为我必须首先将其转换为列表(我这样做是因为其他方法调用 GetMyList
方法,并且不首先转换为列表,数据消失了[因为延迟执行])
我想我在某处读过(现在找不到链接)WCF数据服务不实现IDisposable。如果这是真的,那么 using
语句就没有意义。
Here is an example service:
public class MyWcfDataService : DataService<MyEFModel>
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public IQueryable<GetMyListEF> GetMyList()
{
using (MyEfModel context = this.CurrentDataSource)
{
return context.GetMyListEF().ToList().AsQueryable();
}
}
}
Should I be using the using
statement? It kinda makes IQueryable
pointless since I have to cast it to a List first (I do this because other methods call the GetMyList
method and without casting to a list first, the data is gone [because of deferred execution])
I thought I've read somewhere (can't find the link now) that WCF Data Services don't implement IDisposable. If this is true then the using
statement is pointless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
using
语句将导致您的CurrentDataSource
在using
块的末尾处释放,而不是在DataService
时释放。被处置。因此,问题不是DataService
是否是IDisposable
(它不是),而是MyEfModel
是否是一次性的(它是)。正如失望先生所指出的,如果不是这种情况,编译器将阻止您使用using
语句。这意味着创建新对象时最好使用
using
块。例如:这样您就不会遇到有人在 using 块之后尝试访问 CurrentDataSource 并遇到异常的可能性。
关于您关于 IQueryable 等的其他观点,我通常没有看到 WCF 方法实现 IQueryable,因为它们是通过网络连接使用的。通常首选
IEnumerable
。而且您不会将查询“投射”到列表中,因为这意味着它已经在列表中。您正在评估查询以创建一个列表。然后,您可以将该列表转换为IEnumerable或IQueryable,因为List实现了这些接口。The
using
statement will cause yourCurrentDataSource
to be disposed at the end of theusing
block, not when theDataService
is disposed. Therefore it's not a question of whether theDataService
isIDisposable
(it isn't), but whetherMyEfModel
is disposable (it is). As Mr. Disappointment points out, the compiler would prevent your using theusing
statement if this were not the case.This means that the
using
block is best used when you create a new object. For example:That way you don't run into the possibility that someone will try accessing
CurrentDataSource
after the using block and encountering an exception.Regarding your other point about IQueryable and such, I haven't typically seen WCF methods implement
IQueryable<>
, since they're consumed over a network connection. UsuallyIEnumerable<>
is preferred. And you're not "casting" your query to a List, as that would imply it was already in a list. You are evaluating the query to create a List. Then you might cast that list to an IEnumerable or IQueryable because List implements those interfaces.我最终选择了这个:
不需要
using
语句,因为 WCF 数据服务将在请求结束时处置CurrentDataSource
。使用 IQueryable 还允许利用延迟加载(也称为延迟执行)。这是关于此的另一篇文章:
使用 WCF 数据服务返回 IQueryable 的正确方法和EF
I ended up going with this:
The
using
statement isn't needed because the WCF Data Service will dispose theCurrentDataSource
at the end of the request. UsingIQueryable
also allows utilization of lazy loading aka deferred execution.Here is another post on this:
Proper way to return IQueryable using WCF Data Service and EF