开发者

Can code contracts cross-cut concerns of code?

Is there a way to specify code contract requirements that are cross-cutting, for example a contract at the class level to require an instance variable to be non-null whenever any instance method on that class is invoked, or else the call will fail?

I'm new t开发者_JAVA百科o code contracts and have been skimming the Feb 4, 2011 Code Contracts User Manual (PDF) and the MSDN docs but nothing pops out at me about cross-cutting features. Maybe I'm searching for a feature that code contracts do not have.


I think that what you're looking for are called "object invariants"

From the manual, § 2.3

[ContractInvariantMethod]
private void ObjectInvariant () 
{
   Contract.Invariant ( this.y >= 0 );
   Contract.Invariant ( this.x > this.y );
}

They will be checked after each public method call.
For properly encapsulated values that should be the same as before each call.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜