Share
Embed
Code Contracts API In .NET
Slide deck for my presentation on "Code Contracts API In .NET" in the now defunct Melbourne Patterns Group back in 2009.
Clarence Bakirtzidis
December 14, 2009
Transcript
16 slides
-
Agenda Overview Using Code Contracts Main Features
Runtime Contract Checking Static Contract Checking Demo(s) API Reference Summary Resources Questions * DbC = Design-by-Contract 2 -
Overview What is the Code Contracts API? Library
with Static Methods for DbC It is a spin-off from the learnings of the Spec# project Obtaining and Installing Academic and Commercial Licenses Tools Provided Integration with Visual Studio 3 -
Overview What is Design-by-Contract? Originated from Eiffel
Precondition ◦ Condition that is checked on entry to method Postcondition ◦ Condition that is checked on exit of method Invariant (constrain state of objects) ◦ Condition checked on exit of constructor and all public methods and properties Static (Compile-time) and Runtime checking 4 -
Overview Benefits of Code Contracts Runtime Checking and
Improved Testability Static Verification API Documentation ◦ Examples 5 -
Using Code Contracts .NET 4.0 Part of the
Base Class Library (BCL) No additional references required .NET 3.5 or earlier Separate assembly Microsoft.Contracts.dll Need to add reference to assembly in your projects Contract classes contains in: System.Diagnostics.Contracts namespace Code Snippets for Visual Studio e.g. ci [TAB][TAB] => Contract.Requires(…) 6 -
Using Code Contracts Project Properties Visual Studio Project Properties •
Runtime Checking • Static Checking • Contract Reference Assembly 7 -
Main Features Design-by-Contract Runtime Checking Static Checking
Contract Inheritance Supports Abstract Methods and Interfaces Generate API documentation Hooks into XML documentation and inserts contract requirements (requires, ensures) 8 -
Runtime Contract Checking Configuration options Level of checking
Only public surface contracts Call-site requires Custom rewriter methods Assembly rewriting via ccrewrite.exe Inserts code in project assemblies to enforce contracts at runtime 9 -
Static Contract Checking Configuration options Null values
Array bounds Arithmetic (div-by-zero) Baseline Suppress existing warnings in code base 10 -
API Reference Preconditions Contract.Requires(…) EndContractBlock (“legacy- requires”)
Postconditions Contract.Ensures(…) Contract.Ensures<E>(…) Contract.EnsuresOnThrow<E>(…) Prestate Values Contract.Result<T>() Contract.OldValue<T>(…) Out Parameters Contract.ValueAtReturn<T>(…) Invariants Contract.Invariant(…) Attributes Pure ContractInvariantMethod ContractVerification ContractPublicPropertyName Quantifiers Contract.ForAll Contract.Exists Interfaces and Abstract Methods ContractClass ContractClassFor Other Contract.Assert(…) Contract.Assume(…) ContractException 12 -
Summary Code Contracts bring DbC to the .NET framework
Provides static and runtime checking of: Preconditions Postconditions Object invariants Extends generated XML documentation Some known issues (still work in progress) Build slowdown - will be addressed in future Closures - static checking does not work for closures Edit-Continue does not work with code contract rewriting on No contracts allowed on delegates No contracts on iterators that use “yield” as IL the code changed into different form (workaround exists for this) 13 -
Resources Code Contracts Project http://research.microsoft.com/en-us/projects/ contracts/ MSDN
Forum http://social.msdn.microsoft.com/Forums/en- US/codecontracts/threads Spec# Project http://research.microsoft.com/en-us/projects/ specsharp 15