Using the Code Contracts API for design-by-contract in .NET<br />Melbourne Patterns Group Presentation<br />By Clarence Ba...
Agenda<br />Overview<br />Using Code Contracts<br />Main Features<br />Runtime Contract Checking<br />Static Contract Chec...
Overview<br />What is the Code Contracts API?<br />Library with Static Methods for DbC<br />It is a spin-off from the lear...
Overview<br />What is Design-by-Contract?<br />Originated from Eiffel<br />Precondition<br />Condition that is checked on ...
Overview<br />Benefits of Code Contracts<br />Runtime Checking and Improved Testability<br />Static Verification<br />API ...
Using Code Contracts<br />.NET 4.0<br />Part of the Base Class Library (BCL)<br />No additional references required<br />....
Using Code Contracts<br />Visual Studio Project Properties<br /><ul><li>Runtime Checking
Static Checking
Contract Reference Assembly</li></ul>Project Properties<br />7<br />
Main Features<br />Design-by-Contract<br />Runtime Checking<br />Static Checking<br />Contract Inheritance<br />Supports A...
Runtime Contract Checking<br />Configuration options<br />Level of checking<br />Only public surface contracts<br />Call-s...
Static Contract Checking<br />Configuration options<br />Null values<br />Array bounds<br />Arithmetic (div-by-zero)<br />...
Demo(s)<br />11<br />Demo<br />
API Reference<br />Preconditions<br />Contract.Requires(…)<br />EndContractBlock (“legacy-requires”)<br />Postconditions<b...
Summary<br />Code Contracts bring DbC to the .NET framework<br />Provides static and runtime checking of:<br />Preconditio...
Code Contracts Summary<br />14<br />
Upcoming SlideShare
Loading in …5
×

Code Contracts API In .Net

2,936 views

Published on

Published in: Technology
  • Be the first to comment

Code Contracts API In .Net

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

×