code-contracts

.NET 4 Code Contracts: “requires unproven: source != null”

醉酒当歌 提交于 2019-12-07 04:56:21
问题 I just started using code contracts in my project. However, I have a problem with my repository implementation, which queries my database using the Entity Framework. I have the following method: public IEnumerable<Organization> GetAllOrganizations() { return _uow.CreateSet<Party>().OfType<Organization>().AsEnumerable(); } The method returns a collection containing all organizations in the database, or an empty collection there's not organizations in the database. However, this is not okay

Why can't I add Contract.Requires in an overridden method?

回眸只為那壹抹淺笑 提交于 2019-12-06 20:12:42
问题 I'm using code contract (actually, learning using this). I'm facing something weird to me... I override a method, defined in a 3rd party assembly. I want to add a Contract.Require statement like this: public class MyClass: MyParentClass { protected override void DoIt(MyParameter param) { Contract.Requires<ArgumentNullException>(param != null); this.ExecuteMyTask(param.Something); } protected void ExecuteMyTask(MyParameter param) { Contract.Requires<ArgumentNullException>(param != null); /*

Code contracts on auto-implemented properties

你离开我真会死。 提交于 2019-12-06 18:29:37
问题 Is there any way to put contracts on automatically implemented properties in .NET? (And how if the answer is 'Yes')? (I assume using .NET code contracts from DevLabs) 回答1: Yes, this is possible - all that is needed is to add your contract condition to the [ContractInvariantMethod] method in your class, which then adds the equivalent Requires precondition to the automatic set ter, and a post condition Ensures is added to the get . From section 2.3.1 of the Reference As the example illustrates,

Ensures Unproven via property when implementing interface

走远了吗. 提交于 2019-12-06 11:51:29
I'm trying what, to me, seems like some fairly basic code contracts code. I've reduced it down to the following problem. The following fails the static analysis, with the message CodeContracts: ensures unproven: this.Frozen using System; using System.Diagnostics.Contracts; namespace PlayAreaCollection2010 { public class StrippedContract : IBasic { private bool _frozen = false; public void Freeze() { _frozen = true; } public bool Frozen { get { return _frozen; } } } [ContractClass(typeof(IBasicContract))] public interface IBasic { void Freeze(); bool Frozen { get; } } [ContractClassFor(typeof

Combining code contracts and regex

五迷三道 提交于 2019-12-06 08:15:21
So I have a very simple class with one string as property. This string has to have a certain pattern. I'm trying to enforce this using code contracts. The class looks something like this: class SimpleClass { public Property { get; set; } public SimpleClass(string prop) { Contract.Requires(IsValid(prop)); this.Property = prop; } [ContractInvariantMethod] void ObjectInvariant() { Contract.Invariant(IsValid(Property)); } bool IsValid(string arg) { // Use regex to check if arg is a valid string } } Very straightforward. However, this throws an unreadable exception and another one saying that

Is there a code contract library for JavaScript? [closed]

大憨熊 提交于 2019-12-06 03:54:37
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . There are lots of form validation libraries and jQuery plugins. Though I cannot find a code contract library, for validating function arguments. As an example, to do contract validation in .NET, you could use the outstanding Conditions library . I'm looking for something similar for JavaScript. The closest I can find is Speks , but its for Node.js and is geared for testing, whereas I need to bake the validation

Code Contracts: How to deal with inherited interfaces?

不羁岁月 提交于 2019-12-06 02:03:51
问题 I'm using MS Code Contracts and have run into a snag with using interface inheritance and ContractClassFor attributes. Given these interfaces and contract classes: [ContractClass(typeof(IOneContract))] interface IOne { } [ContractClass(typeof(ITwoContract))] interface ITwo : IOne { } [ContractClassFor(typeof(IOne))] abstract class IOneContract : IOne { } [ContractClassFor(typeof(ITwo))] abstract class ITwoContract : IOneContract, ITwo { } Let's say that IOne and ITwo are substantial

Code Contracts: Invariants in abstract class

淺唱寂寞╮ 提交于 2019-12-05 19:18:14
问题 I have encountered a problem while using Invariants with Code Contracts. I want to define an Invariant within my abstract class but it is simply ignored. The code below shows my interface and the abstract class. [ContractClass(typeof(IPointContract))] interface IPoint { int X { get; } int Y { get; } } [ContractClassFor(typeof(IPoint))] abstract class IPointContract : IPoint { public int X { get { return 0; } } public int Y { get { return 0; } } [ContractInvariantMethod] private void

Controlling Code Contract references in a .nuspec

六眼飞鱼酱① 提交于 2019-12-05 14:21:36
I'm using Code Contracts to generate satellite assemblies for my project. Basically it creates a MyAssembly.Contracts.dll for the project's MyAssembly.dll. This is supposed to be put beside your assembly but not referenced by any app -- it's used only by the contracts tools. I'm trying to include this in my nuget package, but when I then install that package into another app, the Contracts assembly is included as a reference which creates problems. According to the .nuspec reference , this should be as simple as adding a references element, but it seems to be ignored. My current theory is that

Entities used by ORM in combination with CodeContracts - ensure invariants

送分小仙女□ 提交于 2019-12-05 13:05:37
I am currently in the process of adding CodeContracts to my existing code base. One thing that proves difficult is the usage of entities that are hydrated by NHibernate. Assume this simple class: public class Post { private Blog _blog; [Obsolete("Required by NHibernate")] protected Post() { } public Post(Blog blog) { Contract.Requires(blog != null); _blog = blog; } public Blog Blog { get { Contract.Ensures(Contract.Result<Blog>() != null); return _blog; } set { Contract.Requires(value != null); _blog = value; } } [ContractInvariantMethod] private void Invariants() { Contract.Invariant(_blog !=