Archive for Level 400

Code Contracts: Now with IntelliSense

Using the Code Contracts Editor Extensions for Visual Studio 2010, available as a free download as of today, you get code contract details in the IntelliSense pop-​​up of your contracted methods. Currently it only works with C# code.

Very nice!

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Comments off

Easy to fix issue between NServiceBus and Code Contracts

If you use NServiceBus' Configure.With fluent interface, and you use Code Contracts in any assem­blies that your NServiceBus config­u­ration uses, then configure Code Contracts in those assem­blies not to build separate code contract assem­blies, because this confuses NServiceBus.

Configure.With scans all assem­blies for imple­men­ta­tions, and if any imple­men­tation you have also generates code contract assem­blies, then basi­cally you will end up with 2 assem­blies that expose the same classes — one with the actual imple­men­tation, the other with the code contracts. This confuses NServiceBus and you'll get an error:

There is a component already registered for the given key

The simple solution is to go to the "offending" assembly project's Properties, select the Code Contracts tab, and in the Contract Reference Assembly drop­downlist, select "(none)" instead of "Build". The contract code will then be compiled straight into the code assembly rather than sepa­rately, and this will fix the issue with NServiceBus.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Comments (1)

Code Contracts: Static checking doesn't work on Attributes

Code Contracts' static analyzer is a great feature, but of course it does have its limi­ta­tions. Some things it just cannot check at compile-​​time, and other things it can… but doesn't (always).

One example of where the static analyzer could check, but doesn't, is on attribute contracts.

Say you define an attribute with code contracts in it, like so:

using System;
using System.Diagnostics.Contracts;

namespace CodeContractsTest
{
    ///

    /// My super validation attribute.
    /// 

    public class SuperValidationAttribute : Attribute
    {
        public SuperValidationAttribute(string pattern)
        {
            Contract.Requires(!string.IsNullOrEmpty(pattern));
            Contract.Requires(pattern.Contains("$"));
            Pattern = pattern;
        }

        ///

        /// Gets or sets the pattern to use.
        /// 

        public string Pattern { get; set; }
    }
}

Then when you use that attribute, the contracts will not be stat­i­cally checked; i.e., if you use the attribute wrongly, you will not be warned at compile-​​time.

Microsoft is aware of this issue, and hope­fully it will be fixed in an update soon.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Comments (2)