The ContractClassFor
is a dummy class implementation for the interface, with no other purpose than to publish the contracts that the interface demands from, and promises to, its consumers. As can be seen from the corresponding ContractClass
, the interface and its contract class are tightly coupled. (This rather awkward implementation is presumably is because the contracts can't be published directly on the interface, as there is no implementation allowed on an interface). The contracts in the dummy ContractClassFor
are then enforced for all real
implementations of the underlying interface (Also note that only the ContractClassFor
dummy implementation is able to publish contracts for the interface - otherwise different implementations could have different contracts, which wouldn't really make sense.)
The ContractClassFor
class is never instantiated, and you will often find dummy implementations just to get the compiler to compile, e.g.
public string Convert(string text)
{
Contract.Requires(text != null);
Contract.Ensures(Contract.Result<string>() != null);
return default(string); // returns dummy value
}
or
public string Convert(string text)
{
Contract.Requires(text != null);
Contract.Ensures(Contract.Result<string>() != null);
throw new NotImplementedException();
}
etc.