If you are not familiar with the Code Contracts library which is coming out of Microsoft R&D labs, you need to check this pretty cool little library out. As of Vs2010/.Net 4.0 this library will be making the jump out of the R&D labs.
The Code Contracts library is going to provide a way to express coding assumptions in .Net. But what makes this library different from other libraries out there is that they have added in both static analysis and code weaving. Having the ability to include static analysis is a great thing because we can instruct the compiler to provide us feedback if any of our contracts are going to be violated (yes I am 100% aware that even the compiler cannot catch all issue, but even 75% is better than nothing).
Over the next few blog posts we will be taking a look at the topics below.
- Downloading and setting up the contracts library (this post)
- Handling Legacy Code
- Providing Support for Unit Tests
- Utilizing Pre & Post Conditions
- Utilizing Object Invariants
- Performing checking in base classes/abstract classes
If you do not want to wait until Visual Studio 2010 and want to give Code Contracts a spin today you can do this with Visual Studio 2008. Just head out to the download section of the contracts site and grab your copy (at the time of this posting 1.2.20518 was the current build).
Once you have the library installed take a stroll over to C:\Program Files\Microsoft\Contracts to take a look at the installed parts of the library. When looking through the folder structure there are 3 items to take a look at.
- \Documentation – This contains a pretty nice overview of the library in .pdf format
- \Samples – This contains various examples provided by the team to get you started
- \PublicAssemblies – This contains the .dll files you need (either for use in SilverLight or standard apps)
After you peer though the installed component, it is time to setup the library for usage in your project. To do this simply add reference to the Microsoft.Contracts.dll which can be found in the \PublicAssemblies\v3.5 folder (personally i would create a local copy and use the dll, but that is up to you).
Once you have added reference to the library you need to import the System.Diagnostics.Contracts namespace. Now that you have added both the assembly and reference to the assembly we are almost ready to code, but before we do that lets take a look at the configuration options which are available with the contracts library.
Once you have installed the library on your machine you will now see a new tab in the configuration panel of your project .
On this panel is a few different areas that we should take a look at. The first is the section which allows you to control the runtime checking (seen below)
This section allows you to configure to your own liking the way that runtime checking is performed. The default is Full which means all contracts will be checked. However, if you only want to do pre/post conditions you can do that, or you can setup for none, the choice is yours. You can also see there are a few check options you can tweak as well. By default the ‘Assert on Contract Failure’ is selected and this will inform the weaver that it needs to provide a dialog to the user when a contract is violated.
Another area on the pane to pay attention to is the static analysis section (seen below)
This section allows you to setup the exact way you want the static analysis checking to be performed, if you want it to be performed at all. If you do not want the analysis done, simply un-check the ‘Perform Static Contract Checking’ box and you will not get the analysis.
Now that we have seen how to install and setup the contracts library, as well as taking a brief look at how to configure it, it is time to get your code one. As I stated above, over the coming posts we will be exploring in depth some of the various features which are included with the contracts library.
Till next time,
06-08-2009 7:00 AM