Derik Whittaker

Syndication

News


Code Contracts Primer – Part 1: Introduction

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.

  1. \Documentation – This contains a pretty nice overview of the library in .pdf format
  2. \Samples – This contains various examples provided by the team to get you started
  3. \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 image .

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)image

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)
image

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,


Posted 06-08-2009 7:00 AM by Derik Whittaker

[Advertisement]

Comments

Tom de Koning wrote re: Code Contracts Primer – Part 1: Introduction
on 06-08-2009 8:40 AM

Very cool Derik! I'm preparing for a talk on Code contracts this wednesday, keep writing :)

on 06-08-2009 10:46 AM

VS2010 and .NET Framework 4 In-Process Side by Side Part 2 - Common in-proc SxS scenarios Achieving Speedups with Small Parallel Loop Boodies Visual Studio 2010 Performance Part 2: Text Editor A scalable reader/writer scheme with optimistic retry Deploying

alberto wrote re: Code Contracts Primer – Part 1: Introduction
on 06-08-2009 12:36 PM

Unfortunately, static analysis will only be available for VSTS.

Derik Whittaker wrote re: Code Contracts Primer – Part 1: Introduction
on 06-08-2009 12:39 PM

RE Static Analysis in VSTS,

Correct, but we use VSTS but do NOT use TFS

Derik Whittaker wrote Code Contracts Primer – Part 2: Handling Legacy Code
on 06-09-2009 7:49 AM

If you are not familiar with the Code Contracts library which is coming out of Microsoft R&D labs

Derik Whittaker wrote Code Contracts Primer – Part 3: Providing Support for Unit Tests
on 06-13-2009 10:56 AM

If you are not familiar with the Code Contracts library which is coming out of Microsoft R&D labs

Derik Whittaker wrote Code Contracts Primer – Part 4: Utilizing Pre & Post Conditions
on 06-15-2009 3:29 PM

If you are not familiar with the Code Contracts library which is coming out of Microsoft R&D labs

Daniel Teng wrote Weekly links #15
on 06-22-2009 9:59 AM

Tools

Derik Whittaker wrote Code Contracts Primer – Part 5: Utilizing Object Invariants
on 07-01-2009 6:03 AM

If you are not familiar with the Code Contracts library which is coming out of Microsoft R&D labs

Salessi wrote re: Code Contracts Primer – Part 1: Introduction
on 10-10-2009 10:26 PM

If you have to do it, you might as well do it right.,

Unalelian wrote re: Code Contracts Primer – Part 1: Introduction
on 10-12-2009 3:04 AM

Very cute :-)))),

Derik Whittaker wrote Code Contracts Primer – Part 6 Interface Contracts
on 05-14-2010 6:41 AM

*** NOTE *** This post has been a long time coming, I would like to thank Kevin Hazzard for doing a kick

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)