code contracts api in .net

16
USING THE CODE CONTRACTS API FOR DESIGN-BY-CONTRACT IN .NET Melbourne Patterns Group Presentation By Clarence Bakirtzidis ([email protected])

Upload: melbournepatterns

Post on 22-Apr-2015

3.542 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Code Contracts API In .Net

USING THE CODE CONTRACTS API FOR DESIGN-BY-CONTRACT

IN .NET

Melbourne Patterns Group Presentation

By Clarence Bakirtzidis ([email protected])

Page 2: Code Contracts API In .Net

Agenda Overview Using Code Contracts Main Features Runtime Contract Checking Static Contract Checking Demo(s) API Reference Summary Resources Questions

* DbC = Design-by-Contract2

Page 3: Code Contracts API In .Net

Overview

What is the Code Contracts API?Library with Static Methods for DbCIt 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

Page 4: Code Contracts API In .Net

Overview

What is Design-by-Contract?Originated from EiffelPrecondition

○ Condition that is checked on entry to methodPostcondition

○ Condition that is checked on exit of methodInvariant (constrain state of objects)

○ Condition checked on exit of constructor and all public methods and properties

Static (Compile-time) and Runtime checking

4

Page 5: Code Contracts API In .Net

Overview

Benefits of Code ContractsRuntime Checking and Improved TestabilityStatic VerificationAPI Documentation

○ Examples

5

Page 6: Code Contracts API In .Net

Using Code Contracts .NET 4.0

Part of the Base Class Library (BCL)No additional references required

.NET 3.5 or earlierSeparate assembly Microsoft.Contracts.dllNeed to add reference to assembly in your projects

Contract classes contains in:System.Diagnostics.Contracts namespace

Code Snippets for Visual Studioe.g. ci [TAB][TAB] => Contract.Requires(…)

6

Page 7: Code Contracts API In .Net

Using Code Contracts

Project Properties

Visual Studio Project Properties• Runtime Checking• Static Checking• Contract Reference Assembly

7

Page 8: Code Contracts API In .Net

Main Features

Design-by-ContractRuntime CheckingStatic Checking

Contract InheritanceSupports Abstract Methods and Interfaces

Generate API documentationHooks into XML documentation and inserts

contract requirements (requires, ensures)

8

Page 9: Code Contracts API In .Net

Runtime Contract Checking Configuration options

Level of checkingOnly public surface contractsCall-site requiresCustom rewriter methods

Assembly rewriting via ccrewrite.exeInserts code in project assemblies to

enforce contracts at runtime

9

Page 10: Code Contracts API In .Net

Static Contract Checking

Configuration optionsNull valuesArray boundsArithmetic (div-by-zero)

BaselineSuppress existing warnings in code base

10

Page 11: Code Contracts API In .Net

Demo(s)

11

Demo

Page 12: Code Contracts API In .Net

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

Page 13: Code Contracts API In .Net

Summary Code Contracts bring DbC to the .NET framework Provides static and runtime checking of:

PreconditionsPostconditionsObject invariants

Extends generated XML documentation Some known issues (still work in progress)

Build slowdown - will be addressed in futureClosures - static checking does not work for closuresEdit-Continue does not work with code contract rewriting onNo contracts allowed on delegatesNo contracts on iterators that use “yield” as IL the code

changed into different form (workaround exists for this)13

Page 14: Code Contracts API In .Net

Code Contracts Summary

14

Page 16: Code Contracts API In .Net

Questions

16