合同的接口方法需要额外的断言吗? [英] Interface methods with contracts need extra assertion?

查看:63
本文介绍了合同的接口方法需要额外的断言吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我有以下界面:

[ContractClass(typeof(ADHelperContract))]

 公共接口IADHelper {

      IEnumerable的<广告组> GetAllGroupsFromOU(String ouPath);

      IEnumerable的< ADUser便有> GetAllUsersFromGroup(String groupPath);

  }

[ContractClass(typeof(ADHelperContract))]
  public interface IADHelper {
      IEnumerable<ADGroup> GetAllGroupsFromOU( String ouPath );
      IEnumerable<ADUser> GetAllUsersFromGroup( String groupPath );
  }

我添加了以下合约:

[ContractClassFor(typeof(IADHelper))]
$
摘要ADHelperContract类:IADHelper {
$
    public IEnumerable< ADGroup> GetAllGroupsFromOU(字符串ouPath){

        Contract.Requires(!String.IsNullOrEmpty(ouPath));

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

       返回默认值(IEnumerable< ADGroup>);

    }

[ContractClassFor(typeof(IADHelper))]
abstract class ADHelperContract:IADHelper {
    public IEnumerable<ADGroup> GetAllGroupsFromOU( string ouPath ) {
        Contract.Requires( !String.IsNullOrEmpty(ouPath) );
        Contract.Ensures( Contract.Result<IEnumerable<ADGroup>>(  ) != null );
        return default( IEnumerable<ADGroup> );
    }

    public IEnumerable< ADUser> GetAllUsersFromGroup(string groupPath){

        Contract.Requires(!String.IsNullOrEmpty(groupPath));
$
        Contract.Ensures(Contract.Result< IEnumerable< ADUser>>( )!= null);

       返回默认值(IEnumerable< ADUser>);

    } b $ b}

    public IEnumerable<ADUser> GetAllUsersFromGroup( string groupPath ) {
        Contract.Requires( !String.IsNullOrEmpty( groupPath ) );
        Contract.Ensures( Contract.Result<IEnumerable<ADUser>>(  ) != null);
        return default( IEnumerable<ADUser> );
    }
}

现在,我有一个类似于以下类的课程:

Now, I've got a class which looks like this:

公共课ADHelper {

   public IEnumerable< ADGroup> GetAllGroupsFromOU(String ouPath){
$
       Contract.Assume(!String.IsNullOrEmpty(ouPath));
$
       var path = GetLDAPPath(ouPath);

       //更多代码

   }¥b $ b  

   私人字符串GetLDAPPath(字符串路径){

       Contract.Requires(!String.IsNullOrEmpty(path));

       Contract.Ensures(!String.IsNullOrEmpty(Contract.Result< String>( )));

       //一些测试并返回一个字符串

    }
    //更多代码

}

public class ADHelper {
   public IEnumerable<ADGroup> GetAllGroupsFromOU(String ouPath) {
       Contract.Assume( !String.IsNullOrEmpty( ouPath ) );
       var path = GetLDAPPath(ouPath);
       //more code
   }
  
    private string GetLDAPPath( String path) {
       Contract.Requires( !String.IsNullOrEmpty( path ) );
       Contract.Ensures( !String.IsNullOrEmpty(Contract.Result<String>(  ) ));
       //some tests and return a string
    }
    //more code
}

现在,问题是:如果我的接口的GetAllGroupsFromOU方法有一个前置条件,说明ouPath不能为null或者为什么我必须添加断言调用以阻止静态检查器说它在GetLDAPPath调用上无法确定?我欠b $ b我在这里遗漏了什么或者断言是不必要的?

And now, the question: if my interface's GetAllGroupsFromOU method has a pre-condition stating that ouPath can't be null or empty, why must I add the assert call to stop the static checker from saying it can't be sure on the GetLDAPPath call? Am I missing something here or is that assert unecessary?

谢谢。

推荐答案

您好Luis,

Hi Luis,

您的 ADHelper 类不会从 IADHelper 继承。 &NBSP;但假设确实如此,那么您不应该为 ouPath 使用
假设,因为需要
合约将自动继承。

Your ADHelper class doesn't inherit from IADHelper.  But assuming it did, then you shouldn't need to use Assume for ouPath because the Requires contract will be inherited automatically.

- 戴夫


这篇关于合同的接口方法需要额外的断言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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