What is Code Contracts?
In Visual studio 2010, .net 4.0 a new feature called Code Contract is introduced. This helps developers reduce their bugs in coding by either avoid writing buggy code or find that code as soon as possible.
This is a separate tool that we need to install into Visual studio 2010. After installation all the class library projects will have a extra pane called Code Contracts in their properties window.
In Visual studio 2010, .net 4.0 a new feature called Code Contract is introduced. This helps developers reduce their bugs in coding by either avoid writing buggy code or find that code as soon as possible.
This is a separate tool that we need to install into Visual studio 2010. After installation all the class library projects will have an extra pane called Code Contracts in their properties window.
This runs conditionally when CONTRACTS_FULL is set to true.Concept:
The basic concept of Code contract is that, we can check conditions in any method to validation incoming and outgoing values on certain criteria.
For example, to avoid divide by zero error, the Denominator value should not be 0 at any time. So, we in our function we can do a kind of asset where denominator > 0 and we can setup our assessmbly to check this conditions at run time or compile time by changing the Code Contracts properties.
If we catch the wrong values at compile time itself, it reduces our buggy code.
There are three types of conditions we can setup using code contracts. They are,
1. Pre conditions:
Conditions about what must be true at method entry for successful execution of method.
Example:
Contract.Requires(d>0);
2. Post Conditions:
Conditions about what must be true at the end of the method before returning the output.
Example:
Contract.Ensures(Contract.Result
3. Object Invariants:
Conditions about what must be true at all public methods for an object at any point of time.
Example:
[ContractInvariantMethod]
void ObjectInvarient()
{
Contract.Invariant(D>0);
}
Here is my sample class which calculates salary for employee based on the hours they worked on that week.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
namespace CodeContractDemo
{
[ContractClass(typeof(IPayrollCalculatorContract))]
public interface IPayrollCalculator
{
double CalculatePayCheck(double baseRate, int hoursWorked);
double DetermineBaseRate(EmployeeType employeeType);
}
[ContractClassFor(typeof(IPayrollCalculator))]
public sealed class IPayrollCalculatorContract : IPayrollCalculator
{
double IPayrollCalculator.CalculatePayCheck(double baseRate, int hoursWorked)
{
Contract.Requires(baseRate > 0);
Contract.Requires(hoursWorked > 0);
Contract.Ensures(Contract.Result
return default(double);
}
double IPayrollCalculator.DetermineBaseRate(EmployeeType employeeType)
{
//Contract.Ensures(Contract.Result
return default(double);
}
}
public class HourlyPayrollCalculator : IPayrollCalculator
{
public double CalculatePayCheck(double baseRate, int hoursWorked)
{
//Contract.Requires(hoursWorked <= 50);
return baseRate*hoursWorked;
}
public double DetermineBaseRate(EmployeeType employeeType)
{
var baseRate = 0d;
if (employeeType == EmployeeType.Technical )
{
baseRate = 5;
}
else
{
baseRate = 2;
}
return baseRate;
}
}
public class SalaryPayrollCalculator : IPayrollCalculator
{
public double CalculatePayCheck(double baseRate, int hoursWorked)
{
//Contract.Requires(hoursWorked <= 60);
return baseRate * hoursWorked;
}
public double DetermineBaseRate(EmployeeType employeeType)
{
var baseRate = 0d;
if (employeeType == EmployeeType.Technical)
{
baseRate = 20;
}
else
{
baseRate = 10;
}
return baseRate;
}
}
public enum EmployeeType
{
Technical,
NonTechnical
}
}
Great Article
thanks for share something new ..