开发者

how to config Code Contracts in Interface

I can't configurate Code Contracts in my class. I've followed the documentation and the example but it doesn't work.

I would to insert Code Contracts condition into my interface, here my code

The interface

 [Con开发者_高级运维tractClass(typeof(ArticleBLLContract))]
 public interface IArticleBLL
 {
    int getArticleNSheet(string IdBox);
    IEnumerable<IArticle> getArticleValue(string IdBox, string IdLanguage);
}

The Contract Class

[ContractClassFor(typeof(IArticleBLL))]
public sealed class ArticleBLLContract : IArticleBLL
{
    int IArticleBLL.getArticleNSheet(string IdBox)
    {
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox),"IdBox has no valid value");                        

        return default(int);
    }

    IEnumerable<Base.Article.IArticle> IArticleBLL.getArticleValue(string IdBox, string IdLanguage)
    {
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox), "IdBox has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdLanguage), "IdLanguagehas no valid value");

        Contract.Ensures(Contract.Result<IEnumerable<Base.Article.IArticle>>() != null, "Return value is out of Range");

        return default(IEnumerable<Base.Article.IArticle>);
    }        
}

The class to apply Contract

public class ArticleBLL : IArticleBLL
{

    public int getArticlNSheet(string IdBox)
    {
        try
        {
            return _Dal...
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public IEnumerable<IArticle> getArticleValue(string IdBox, string IdLanguage)
    {
        IEnumerable<IArticle> article = null;

        try
        {
            article = _Dal...

            return article;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

I've tried to insert a breakpoint in this line

Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox),"IdBox has no valid value");  

but when I call the method it never passes here This is my project configuration

how to config Code Contracts in Interface

Is there something wrong?

Thank you!


Assembly Mode in your configuration should be "Standard Contract Requires".


Total re-write, based on comments so far

I've created a class library project, and put the interface and its contract class in there. I've set it to "Standard Contract Requires", runtime Pre and Post checks, and Build Contract Reference Assembly (I've set identical options for Debug and Release).

I've then got a console application, with a class implementing the interface, and set "Standard Contract Requires", runtime Pre and Post checks (again, set identically between Debug and Release).

Running it in either Debug or Release mode, I'm getting an ArgumentOutOfRangeException when trying to call getArticleNSheet.

With the obvious exception of switching to "Standard Contract Requires", what in the above doesn't match your current setup?


And, in fact, I was in error previously. With "Standard Contract Requires", I'm actually able to hit a breakpoint in the contract class, when debugging. I'm not sure by what Wizardry it's able to do this - since it isn't literally running the code in that class - as evidenced by the fact that you can re-write the method in the contract class as:

    int IArticleBLL.getArticleNSheet(string IdBox)
    {
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(IdBox), "IdBox has no valid value");

        throw new NotImplementedException();
    }

You can put a breakpoint on the Contract.Requires line, and it seems to hit it (after a warning about file mismatches, probably due to the re-writer). But assuming you've passed a non-empty string, it doesn't then throw a NotImplementedException.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜