.NET 4 code合同:"要求得到证实:源=空"! [英] .NET 4 Code Contracts: "requires unproven: source != null"

查看:125
本文介绍了.NET 4 code合同:"要求得到证实:源=空"!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用我的项目code合同。但是,我有我的仓库实现,其中查询使用实体框架我的数据库有问题。

I just started using code contracts in my project. However, I have a problem with my repository implementation, which queries my database using the Entity Framework.

我有以下方法:

public IEnumerable<Organization> GetAllOrganizations()
{
    return _uow.CreateSet<Party>().OfType<Organization>().AsEnumerable();
}

该方法返回一个包含所有组织数据库中的一个集合,或一个空的集合有没有组织数据库。

The method returns a collection containing all organizations in the database, or an empty collection there's not organizations in the database.

不过,这是不行的,根据codeContracts,这给我的错误:需要得到证实:源= NULL

However, this is not okay according to CodeContracts, which give me the error: "requires unproven: source != null"

它是什么想告诉我?我能满足code合同利用Contract.Assume,假定它总是会找到的东西,但我需要做的,在从数据库中读取数据的所有方法。

What is it trying to tell me? I can satisfy code contracts by using Contract.Assume, to assume that it would always find something, but then I need to do that in all methods that reads data from the database.

我失去了一些东西在这里,或者是预期的行为,当你正在使用的数据库和LINQ?

Am I missing something here, or is it intended behavior when you're working with databases and LINQ?

推荐答案

我的猜测是,该方法CreateSet,OfType和AsEnumerable一个声明为与此参数称为源的扩展方式,和codeContrcts不能证明它不是空。另外,不要你需要添加一个要求条款,订明_uow是进入非空?

My guess is that one of the methods CreateSet, OfType and AsEnumerable is declared as an extension method with a this-parameter called "source", and CodeContrcts can't prove it is not null. Also, don't you need to add a Requires clause to specify that _uow is non-null on entry?

CreateSet似乎是因为它不出现MSDN中的扩展方法。如果该方法不应该返回null,您可以通过将本合同执行本 CreateSet

CreateSet appears to be the extension method since it doesn't appear in MSDN. If the method should never return null, you can enforce this by adding this contract to CreateSet:

Contract.Ensures(Contract.Result<T>() != null);

在codeContracts分析认为这条规则,它会把它作为证据,证明该输入 OfType 将不能为空,并警告应该消失。

When the CodeContracts analyzer sees this rule, it will take it as proof that the input to OfType will not be null, and the warning should go away.

这篇关于.NET 4 code合同:&QUOT;要求得到证实:源=空&QUOT;!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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