识别维持不变量的方法 [英] Identifying methods that maintain the invariant

查看:76
本文介绍了识别维持不变量的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有相当长的不变量的课程。不幸的是,我得到了"建议确保"将不变量的每个条件添加到每个属性的每个get访问器中(也可能添加到其他方法中 - "建议确保"的列表对于
来说太长了以确定)。对于我认为应该是纯粹的东西,这有很多确保陈述!有没有办法简单地说"这保持不变"?


我认为kludge将编写一个函数来测试不变量,并将其写为契约不变量,所以然后我也可以将函数调用为get确保的函数,但这会从它所属的位置删除契约。它反而破坏了这一点。


以下是代码摘录:

 [ContractInvariantMethod] 
private void ProjectInvariant ()
{
Contract.Invariant(_entities!= null);
Contract.Invariant(_name!= null);
Contract.Invariant(_annotations!= null);
}

private Dictionary< string,Entity> _entities;
public Dictionary< string,Entity>实体
{
获得
{
Contract.Ensures(Contract.Result< Dictionary< string,Entity>>()!= null);
返回_实体;
}
私人套餐
{
Contract.Requires(value!= null);
Contract.Ensures(实体==价值);
_entities = value;
}
}

我得到的建议如下:


CodeContracts:建议确保:Contract.Ensures(this._annotations!= null);
$
CodeContracts:建议确保:Contract.Ensures(this._name!= null);



我是否真的必须在每个(纯!)访问者上重复列表?

解决方案


我无法使用您提供的代码进行复制。 即使将
警告级别设置为 hi ,我也完全没有警告。 您能否提供
简短但完整的计划来重现警告?


这是我的完整程序: 

 using System.Collections.Generic; 
使用System.Diagnostics.Contracts;

名称空间CCTest
{
class NonNullInvariantsAndAccessors
{
public Dictionary< string,Entity>实体
{
获得
{
Contract.Ensures(Contract.Result< Dictionary< string,Entity>>()!= null);
返回_实体;
}
私人套餐
{
Contract.Requires(value!= null);
Contract.Ensures(实体==价值);

_entities = value;
}
}

公共字符串名称
{
get
{
return _name;
}
私人套餐
{
Contract.Requires(value!= null);
Contract.Ensures(Name == _name);

_name = value;
}
}

公共对象注释
{
get
{
return _annotations;
}
私人套餐
{
Contract.Requires(value!= null);
Contract.Ensures(Annotations == _annotations);

_annotations = value;
}
}

private Dictionary< string,Entity> _entities;
private string _name;
私有对象_注释;

public NonNullInvariantsAndAccessors()
{
_entities = new Dictionary< string,Entity>();
_name ="" ;;
_annotations = new object();
}

[ContractInvariantMethod]
[System.Diagnostics.CodeAnalysis.SuppressMessage(" Microsoft.Performance"," CA1822:MarkMembersAsStatic",Justification =" code for Code)合约。"))
private void ObjectInvariant()
{
Contract.Invariant(_entities!= null);
Contract.Invariant(_name!= null);
Contract.Invariant(_annotations!= null);
}
}

类实体
{
}
}

- 戴夫


I have a class with a fairly long invariant. Unfortunately I'm getting "suggested ensures" to add every condition of the invariant to every get accessor of every property (and maybe to other methods too -- the list of "suggested ensures" is too long for me to be sure). That's a lot of ensures statements for something that I thought was supposed to be pure anyway! Is there any way to simply say "this maintains the invariant"?

I suppose the kludge would be to write a function to test the invariant, and write that as the contract invariant, so I can then also call the function as what the get ensures, but that removes the contract from where it belongs. it rather spoils the point.

Here's an extract from the code:

        [ContractInvariantMethod]
        private void ProjectInvariant()
        {
            Contract.Invariant(_entities != null);
            Contract.Invariant(_name != null);
            Contract.Invariant(_annotations != null);
        }

        private Dictionary<string, Entity> _entities;
        public Dictionary<string, Entity> Entities 
        {
            get
            {
                Contract.Ensures(Contract.Result<Dictionary<string, Entity> >() != null);
                return _entities;
            }
            private set
            {
                Contract.Requires(value != null);
                Contract.Ensures(Entities == value);
                _entities = value;
            }
        }

I'm getting suggestions like:

CodeContracts: Suggested ensures: Contract.Ensures(this._annotations != null);
CodeContracts: Suggested ensures: Contract.Ensures(this._name != null);

Do I really have to repeat the list on every (pure!) accessor?

解决方案

Hi,

I can't repro with the code you provided.  I get no warnings at all, even with Warning Level set to hi.  Could you provide a short but complete program that reproduces the warnings?

Here's my complete program: 

using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace CCTest
{
	class NonNullInvariantsAndAccessors
	{
		public Dictionary<string, Entity> Entities
		{
			get
			{
				Contract.Ensures(Contract.Result<Dictionary<string, Entity>>() != null);
				return _entities;
			}
			private set
			{
				Contract.Requires(value != null);
				Contract.Ensures(Entities == value);

				_entities = value;
			}
		}

		public string Name
		{
			get
			{
				return _name;
			}
			private set
			{
				Contract.Requires(value != null);
				Contract.Ensures(Name == _name);

				_name = value;
			}
		}

		public object Annotations
		{
			get
			{
				return _annotations;
			}
			private set
			{
				Contract.Requires(value != null);
				Contract.Ensures(Annotations == _annotations);

				_annotations = value;
			}
		}

		private Dictionary<string, Entity> _entities;
		private string _name;
		private object _annotations;

		public NonNullInvariantsAndAccessors()
		{
			_entities = new Dictionary<string, Entity>();
			_name = "";
			_annotations = new object();
		}

		[ContractInvariantMethod]
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
		private void ObjectInvariant()
		{
			Contract.Invariant(_entities != null);
			Contract.Invariant(_name != null);
			Contract.Invariant(_annotations != null);
		}
	}

	class Entity
	{
	}
}

- Dave


这篇关于识别维持不变量的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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