DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Code Contracts – Generating documentation

Denzel D. user avatar by
Denzel D.
·
Jul. 17, 10 · Interview
Like (0)
Save
Tweet
Share
5.41K Views

Join the DZone community and get the full member experience.

Join For Free

As any other code element, code contracts need to be somehow documented to better understand their purpose in the existing code and to be able to design code that would interact with existing structures.

There are two ways to generate XML documentation for existing code contracts. The first one and the easiest one is to use the tools provided in Visual Studio. For example, I have this sample console application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Car car = new Car();
car.ChangeState();
Console.Read();
}
}

class Car
{
public bool isWorking = true;
public bool isNot = true;

[ContractInvariantMethod]
void CheckState()
{
Contract.Invariant(isWorking);
}

public void ChangeState()
{
Contract.Requires(isNot);
Random random = new Random();
double result = random.NextDouble();
isWorking = Convert.ToBoolean(Math.Round(result));
}
}
}

It has an invariant implemented as well as a Contract.Requires call.

Before generating the contract documentation, the general code XML documentation should be generated. In Visual Studio, this option is enabled in the application properties, in the Build tab:

In the same Properties dialog, in the Code Contracts tab there is the Contract Reference Assembly group box. That is the place where you need to look to generate the XML docs for the contracts that are present in the current assembly.

In order to be able to generate the XML file, you would need to build the reference assembly. Select the Build option from the list and check the Emit contracts into XML doc file. No contract documentation can be generated without this.

This will generate a CodeContracts folder inside the project folder, as well as a [AssemblyName].Contracts.dll – that is the reference assembly that is needed. Once you build the solution, inside the folder you will also see [AssemblyName].XML that will contain the XML representation of the documented code parts, contracts included.

For the above sample application, the XML will look like this:

<?xml version="1.0"?>
<doc>
<assembly>
<name>ConsoleApplication</name>
</assembly>
<members>
<member name="T:ConsoleApplication.Car">
<invariant>isWorking</invariant>
</member>
<member name="M:ConsoleApplication.Car.ChangeState">
<requires>isNot</requires>
</member>
</members>
</doc>

You can clearly see where the contracts are placed, under which methods and classes as well as their type.

IMPORTANT NOTE: Do not name your assembly so that it ends in .Contracts. This will confuse the contract reference assembly builder and will eventually result in invalid data being generated.

The second way to generate XML documentation for existing contracts requires a bit more work, but eventually gives you more control over the process.

Once you install Code Contracts, you automatically get the ccdocgen tool, that generates XML–formatted documentation for the contract reference assembly. And this applies to the contract assembly only – don’t try this on your base assembly.

The ccdocgen is located in your %PROGRAMFILES%\Microsoft\Contracts\Bin folder (%PROGRAMFILES(X86)%\Microsoft\Contracts\Bin for 64-bit systems). Via this tool, the XML file can be generated by following this pattern:

ccdocgen –assembly [ReferenceAssemblyPath] –XmlFile [NameForXML]

The XML file will be generated automatically in the folder where the command prompt is currently pointing to. The XmlFile parameter is optional but I find it extremely useful because sometimes I want to give the XML file a more descriptive name, when I test multiple builds.

NOTE: In case you need to generate the reference assembly, you can use the ccrefgen tool, located in the same Bin folder with ccdocgen.

Documentation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Testing
  • What Are the Different Types of API Testing?
  • A Gentle Introduction to Kubernetes
  • Java REST API Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: