{"id":447,"date":"2009-09-13T12:27:27","date_gmt":"2009-09-13T11:27:27","guid":{"rendered":"http:\/\/www.leading-edge-dev.de\/?p=447"},"modified":"2010-07-10T16:20:29","modified_gmt":"2010-07-10T15:20:29","slug":"code-contracts-sandcastle-integration","status":"publish","type":"post","link":"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts\/code-contracts-sandcastle-integration","title":{"rendered":"Code Contracts #8: Sandcastle integration"},"content":{"rendered":"<p>Release 1.2.20903 (4 Sep 2009) of <a href=\"http:\/\/msdn.microsoft.com\/de-de\/devlabs\/dd491992%28en-us%29.aspx\">Code Contracts<\/a> comes with a nice integration of contracts on classes and methods into <a href=\"http:\/\/www.codeplex.com\/Sandcastle\">Sandcastle<\/a> documentation! By this, Code Contracts achieve an important goal: <em>real<\/em> &#8222;checked documentation&#8220;. This short article gives an overview of how to get your Sandcastle installation to handle contracts.<\/p>\n<h3>1) Install Sandcastle (+DocProject)<\/h3>\n<p>At first, of course you need a valid Sandcastle installation. Just go to the <a href=\"http:\/\/www.codeplex.com\/Sandcastle\">Sandcastle project page<\/a>, download the latest release and install it. I recommend installing <a href=\"http:\/\/www.codeplex.com\/DocProject\">DocProject for Sandcastle<\/a> and <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?familyid=00535334-C8A6-452F-9AA0-D597D16580CC&amp;displaylang=en\">HTML Help Workshop and Documentation<\/a> as well. This gives you a nice Visual Studio integration for creating documentation by Sandcastle.<\/p>\n<h3>2) Patch Sandcastle with Code Contracts files<\/h3>\n<p>Code Contracts documentation syntax is currently not included in the Sandcastle project. In order to make Sandcastle aware of the Code Contracts documentation items, you have to patch the Sandcastle installation with the Code Contracts files. For this, there exists a folder &#8222;<em>Sandcastle<\/em>&#8220; in the Code Contracts installation directory (default is &#8222;<em>Program Files\\Microsoft\\Contracts\\Sandcastle<\/em>&#8222;), which contains a file &#8222;<em>Sandcastle.zip<\/em>&#8222;.<\/p>\n<p>If you&#8217;ve installed Sandcastle from the MSI package, just copy the contents from the &#8222;<em>msi\\vs2005<\/em>&#8220; folder of the UIP file to the following location of your Sandcastle installation: &#8222;<em>Program Files\\Sandcastle\\Presentation\\vs2005\\<\/em>&#8222;. Now you&#8217;re done and ready to generate documentation for your contracts!<\/p>\n<h3>3) Set up your project<\/h3>\n<p>First, if you want to generate documentation for a project in Visual Studio, go to the project properties and in the &#8222;Build&#8220; option pane, check the <strong>XML documentation file<\/strong> option:<\/p>\n<p><a href=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_xmldoc_option.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-449 alignnone\" title=\"Activate the &quot;XML documentation file&quot; option\" src=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_xmldoc_option-300x241.png\" alt=\"Activate the &quot;XML documentation file&quot; option\" width=\"300\" height=\"241\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_xmldoc_option-300x241.png 300w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_xmldoc_option.png 747w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Then, to get the contracts injected into the generated XML doc file, go to the &#8222;Code Contracts&#8220; option pane and select both the <strong>Build a Contract Reference Assembly<\/strong> and the <strong>Emit contracts into XML doc file<\/strong> options:<\/p>\n<p><a href=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_ccdoc_option.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-450\" title=\"Select generation of Code Contracts documentation\" src=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_ccdoc_option-300x215.png\" alt=\"Select generation of Code Contracts documentation\" width=\"300\" height=\"215\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_ccdoc_option-300x215.png 300w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_ccdoc_option.png 706w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now, when you build your project, first the documentation of your project&#8217;s components will be created as XML file and then the contracts documentation will be injected in the same file.<\/p>\n<h3>4) Create your documentation!<\/h3>\n<p>For demonstation purposes, I&#8217;ve generated a project (including a new solution) <strong>AccountExample<\/strong> in Visual Studio, which holds just one class <code>Account<\/code>. This example class contains some contracts and looks as follows:<\/p>\n<pre class=\"brush:csharp\">\/\/\/ &lt;summary&gt;\r\n\/\/\/ Represents an account, to which you can deposit and from which you can withdraw money.\r\n\/\/\/ &lt;\/summary&gt;\r\npublic class Account\r\n{\r\n    private float _balance;\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ The current balance of the account.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;value&gt;The account's balance.&lt;\/value&gt;\r\n    public float Balance\r\n    {\r\n        get { return _balance; }\r\n        private set\r\n        {\r\n            Contract.Requires(value &gt;= 0);\r\n            _balance = value;\r\n        }\r\n    }\r\n\r\n    [ContractInvariantMethod]\r\n    protected void ClassInvariants()\r\n    {\r\n        Contract.Invariant(_balance &gt;= 0);\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Initializes a new instance of the &lt;see cref=\"Account\"\/&gt; class.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"balanceInitial\"&gt;The initial balance, which will be set.&lt;\/param&gt;\r\n    public Account(float balanceInitial)\r\n    {\r\n        Contract.Requires(balanceInitial &gt;= 0);\r\n        Contract.Ensures(Balance == balanceInitial);\r\n\r\n        Balance = balanceInitial;\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Deposits the specified amount to the account.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"amount\"&gt;The amount to deposit.&lt;\/param&gt;\r\n    public void Deposit(float amount)\r\n    {\r\n        Contract.Requires(amount &gt; 0);\r\n        Contract.Ensures(Balance == (Contract.OldValue(Balance) + amount));\r\n\r\n        Balance += amount;\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ Withdraws the specified amount from the account.\r\n    \/\/\/ &lt;\/summary&gt;\r\n    \/\/\/ &lt;param name=\"amount\"&gt;The amount to withdraw.&lt;\/param&gt;\r\n    public void Withdraw(float amount)\r\n    {\r\n        Contract.Requires(amount &gt; 0);\r\n        Contract.Requires(amount &lt;= Balance);\r\n        Contract.Ensures(Balance == (Contract.OldValue(Balance) - amount));\r\n\r\n        Balance -= amount;\r\n    }\r\n}<\/pre>\n<p>Moreover, I&#8217;ve added a new <strong>DocProject<\/strong> to my solution, where I&#8217;ve chosen the &#8222;<em>AccountExample<\/em>&#8220; project for generating the documentation from. With that we&#8217;re ready to roll!<\/p>\n<p>When building the whole solution, first the assembly of the &#8222;<em>AccountExample<\/em>&#8220; project is built. In this step, the following XML documentation file is extracted as well (note the injected contract tags &#8211; <code>requires<\/code>, <code>ensures<\/code> and <code>invariant<\/code>):<\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;doc&gt;\r\n  &lt;assembly&gt;\r\n    &lt;name&gt;AccountExample&lt;\/name&gt;\r\n  &lt;\/assembly&gt;\r\n  &lt;members&gt;\r\n    &lt;member name=\"T:AccountExample.Account\"&gt;\r\n      &lt;summary&gt;\r\n            Represents an account, to which you can deposit and from which you can withdraw money.\r\n            &lt;\/summary&gt;\r\n      &lt;invariant&gt;_balance &gt;= 0&lt;\/invariant&gt;\r\n    &lt;\/member&gt;\r\n    &lt;member name=\"M:AccountExample.Account.#ctor(System.Single)\"&gt;\r\n      &lt;summary&gt;\r\n            Initializes a new instance of the &lt;see cref=\"T:AccountExample.Account\" \/&gt; class.\r\n            &lt;\/summary&gt;\r\n      &lt;param name=\"balanceInitial\"&gt;The initial balance, which will be set.&lt;\/param&gt;\r\n      &lt;requires&gt;balanceInitial &gt;= 0&lt;\/requires&gt;\r\n      &lt;ensures&gt;Balance == balanceInitial&lt;\/ensures&gt;\r\n    &lt;\/member&gt;\r\n    &lt;member name=\"M:AccountExample.Account.Deposit(System.Single)\"&gt;\r\n      &lt;summary&gt;\r\n            Deposits the specified amount to the account.\r\n            &lt;\/summary&gt;\r\n      &lt;param name=\"amount\"&gt;The amount to deposit.&lt;\/param&gt;\r\n      &lt;requires&gt;amount &gt; 0&lt;\/requires&gt;\r\n      &lt;ensures&gt;Balance == (Contract.OldValue(Balance) + amount)&lt;\/ensures&gt;\r\n    &lt;\/member&gt;\r\n    &lt;member name=\"M:AccountExample.Account.Withdraw(System.Single)\"&gt;\r\n      &lt;summary&gt;\r\n            Withdraws the specified amount from the account.\r\n            &lt;\/summary&gt;\r\n      &lt;param name=\"amount\"&gt;The amount to withdraw.&lt;\/param&gt;\r\n      &lt;requires&gt;amount &gt; 0&lt;\/requires&gt;\r\n      &lt;requires&gt;amount &lt;= Balance&lt;\/requires&gt;\r\n      &lt;ensures&gt;Balance == (Contract.OldValue(Balance) - amount)&lt;\/ensures&gt;\r\n    &lt;\/member&gt;\r\n    &lt;member name=\"P:AccountExample.Account.Balance\"&gt;\r\n      &lt;summary&gt;\r\n            The current balance of the account.\r\n            &lt;\/summary&gt;\r\n      &lt;value&gt;The account's balance.&lt;\/value&gt;\r\n      &lt;setter&gt;\r\n        &lt;requires&gt;value &gt;= 0&lt;\/requires&gt;\r\n      &lt;\/setter&gt;\r\n    &lt;\/member&gt;\r\n  &lt;\/members&gt;\r\n&lt;\/doc&gt;<\/pre>\n<p>In a second step of the build process, the DocProject generates the documentation for the &#8222;<em>AccountExample<\/em>&#8220; project by taking its XML documentation file. When finished, we&#8217;ll have a nice CHM file with the documentation of our example project.<\/p>\n<p>In this documentation we can see our contracts now. On class level, we have our <strong>invariant<\/strong>:<\/p>\n<p><a href=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_chm_class.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-452\" title=\"Class invariants in Sandcastle documentation\" src=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_chm_class-300x201.png\" alt=\"Class invariants in Sandcastle documentation\" width=\"300\" height=\"201\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_chm_class-300x201.png 300w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_chm_class.png 788w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>And on method (+properties) level, we get the <strong>pre-<\/strong> and <strong>postconditions<\/strong>:<\/p>\n<p><a href=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_chm_method.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-453\" title=\"Method contracts in Sandcastle documentation\" src=\"http:\/\/www.leading-edge-dev.de\/wp-content\/uploads\/2009\/09\/cc_chm_method-300x204.png\" alt=\"Method contracts in Sandcastle documentation\" width=\"300\" height=\"204\" srcset=\"https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_chm_method-300x204.png 300w, https:\/\/www.minddriven.de\/wp-content\/uploads\/2009\/09\/cc_chm_method.png 821w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>As you can see, contracts in documentation are really valuable. Including them in Sandcastle documentation brings the claim of &#8222;checked documentation&#8220; to life! This really helps other developers to understand the intent of your classes by looking at the documentation. And by automatically generating documentation out of your code-based contracts, you can be sure that docu and code do not run out of sync. This really puts more value on Code Contracts and is a real practical advantage!<\/p>\n<p><a href=\"http:\/\/www.dotnetkicks.com\/kick\/?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d447\"><img decoding=\"async\" src=\"http:\/\/www.dotnetkicks.com\/Services\/Images\/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.leading-edge-dev.de%2f%3fp%3d447\" border=\"0\" alt=\"kick it on DotNetKicks.com\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Release 1.2.20903 (4 Sep 2009) of Code Contracts comes with a nice integration of contracts on classes and methods into Sandcastle documentation! By this, Code Contracts achieve an important goal: real &#8222;checked documentation&#8220;. This short article gives an overview of how to get your Sandcastle installation to handle contracts. 1) Install Sandcastle (+DocProject) At first, &hellip; <a href=\"https:\/\/www.minddriven.de\/index.php\/technology\/dot-net\/code-contracts\/code-contracts-sandcastle-integration\" class=\"more-link\"><span class=\"screen-reader-text\">Code Contracts #8: Sandcastle integration<\/span> weiterlesen<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[319,170,174,173,172,171,312],"class_list":["post-447","post","type-post","status-publish","format-standard","hentry","category-code-contracts","tag-code-contracts","tag-contracts","tag-docproject","tag-documentation","tag-integration","tag-sandcastle","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/comments?post=447"}],"version-history":[{"count":16,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/447\/revisions"}],"predecessor-version":[{"id":953,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/posts\/447\/revisions\/953"}],"wp:attachment":[{"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/categories?post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.minddriven.de\/index.php\/wp-json\/wp\/v2\/tags?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}