Articles tagués check at build time

Code Contract, a way to control authorized values input output method or in interface at building time ! – Microsoft Static Check

From Microsoft research, with code contract, it is allowed to make verification on input, ouput arguments on methods and make other check on Interface.

First check in object explorer if you have Microsoft.Contracts.

1/ if you have an old Visual studio download Code contract assembly on download panel: http://research.microsoft.com/en-us/projects/contracts/

downloadcodecontract

run MSI installer as follows:

msicodecontract

Add a reference on Microsoft.Contracts assembly (which implements Data Contract you have just installed before 😉 :

codecontractreference

in object explorer, (double click on Microsoft.Contracts to run it), you will see the 2 namespaces and classes contained:

codecontractnamespace

2/Now you have Code Contract Assembly

in BizProcess method, use Contract class and call Requires method to check input value (here we check that b is different of 3 ;-):

Contractrequires

Go to properties solution and in Code Contracts panel (left side), Check Perform Static Contract Checking in order to make checks on build time 😉

propertiesCodeContracts

when you build solution, you will see a warning as below because a business rule is violated (b different of 3 in our sample)

inactionCodeContracts

Code contract is very interesting to make some check at build time because at runtime it is too late… 😉

You can also make check on Interface by using attributes ContractClass and ContractClassFor

For sure when data are knowing at runtime, you will make your check in code.

, , ,

1 commentaire

Microsoft System Center 2012

All System Center 2012 products, Service Manager, App-Controller, Orchestrator, Virtual Machine Manager, Configuration Manager, Data Protection Manager, Operation Manager -Boutougha Hassan

Microsoft Windows Server 2012

IT - Windows server 2012 - Hassan Boutougha

Microsoft Hyper-V

IT - Hyper-V - Hassan Boutougha

Microsoft SQL Server 2012

IT - SQL Server 2012 - Hassan Boutougha