代码契约与不使用它的框架一起
我有这样的代码:
return session.Query<Payment>()
.OrderBy(payment => payment.Created)
.Skip((page - 1)*pageSize)
.Take(pageSize)
.ToArray();
由于 NHibernate 的 Query 方法没有任何 Contract.Ensures,我收到此警告:
CodeContracts:需要未经验证:源!= null
为了解决这个问题,我写了这样的:
var query = session.Query<Payment>();
Contract.Assume(query != null);
return query
.OrderBy(payment => payment.Created)
.Skip((page - 1)*pageSize)
.Take(pageSize)
.ToArray();
这是正确的方法吗?现在,Resharper 认为查询可以为 null,因为我将它与 null 进行比较。
可能对标有“NotNull”属性的实体进行“null”赋值
I've got this code:
return session.Query<Payment>()
.OrderBy(payment => payment.Created)
.Skip((page - 1)*pageSize)
.Take(pageSize)
.ToArray();
Since NHibernate's Query method don't have any Contract.Ensures, I get this warning:
CodeContracts: requires unproven: source != null
To fix it, I write this:
var query = session.Query<Payment>();
Contract.Assume(query != null);
return query
.OrderBy(payment => payment.Created)
.Skip((page - 1)*pageSize)
.Take(pageSize)
.ToArray();
Is this the right way to do it? Now Resharper thinks query can be null since I compare it to null.
Possible 'null' assignment to entity marked with 'NotNull' attribute
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是正确的做法。默认情况下,Resharper 不理解代码契约,请查看 这个问题来解决这个问题。
您可能想在库中创建一个
Query
方法,该方法仅调用 NHibernate 的Query
但也有一个Ensures
契约,以避免您使用假设
无处不在。类似的东西(我猜这里的类型):
Yes, that is the right way to do it. Resharper doesn't understand Code Contracts by default, check out this question to fix that.
You might want to make a
Query
method inside your library which just calls NHibernate'sQuery
but also has anEnsures
contract, to save you having to useAssume
everywhere.Something like (I'm guessing the types here):