Archives de la catégorie Code Contract
Code Contract, a way to control authorized values input output method or in interface at building time ! – Microsoft Static Check
Publié par hassanboutougha dans Code Contract, Microsoft Infrastructure Framework - Unity MEF Moles ... - IoC AOP ... le décembre 16, 2012
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/
run MSI installer as follows:
Add a reference on Microsoft.Contracts assembly (which implements Data Contract you have just installed before 😉 :
in object explorer, (double click on Microsoft.Contracts to run it), you will see the 2 namespaces and classes contained:
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 ;-):
Go to properties solution and in Code Contracts panel (left side), Check Perform Static Contract Checking in order to make checks on build time 😉
when you build solution, you will see a warning as below because a business rule is violated (b different of 3 in our sample)
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.