8

I have started using Code Contracts and have found that it makes it difficult to immediately spot the 'guts' of a method.

Take this (very simple) example:

public static void UserAddNew(string domain, string username, string displayName)
{
    Contract.Assert(!string.IsNullOrWhiteSpace(domain));
    Contract.Assert(!string.IsNullOrWhiteSpace(username));
    Contract.Assert(!string.IsNullOrWhiteSpace(displayName));

    LinqDal.User.UserAddNew(domain, username, displayName);
}

Now I'm tempted to put the contracts in a region, so that they can be hidden away, but then I'm concerned that I'm losing a nice advantage of being able to glance at the method and see what it expects.

What do you do to keep your contracts 'tidy'? Or am I just being too picky?

4

1 回答 1

6

查看 ContractClass 和 ContractClassFor 属性。这允许您在单独的程序集中使用代码协定编写类。这使您可以拥有可用于开发工作的合约,不会使您的代码混乱,也意味着您不必使用实时代码部署合约:

合约类属性

属性的合同类

于 2011-02-15T09:07:35.150 回答