c# - Putting Contracts for Constructor Arguments -
assume have interface follows contract class
[contractclassfor(typeof(custom))] public abstract class customcontract : custom { public string getperson(int i) { contract.requires(i > 0); contract.ensures(!string.isnullorempty(contract.result<string>())); return default(string); } public string name { get; set; } } [contractclass(typeof(customcontract))] public interface custom { string getperson(int i); string name { get; set; } }
implementation like
public class customprocessor: custom { public customprocessor(isomeinterface obj, string logname) { if(null == obj) throw new argumentnullexception(...); ... } public getperson(int i) { ... } }
does make sense replace throw new argumentnullexception
in constructor contract.requires(obj != null).
contracts supposed defined interface , since constructor part of implementation , not interface leaned towards current approach. practice ?
to out of code contracts, yes, makes sense replace argumentnullexception
throws contract.requires
.
this right given fact you're using interface , contract class. there no other place validate constructor parameters other in constructor itself.
if use argumentnullexception
approach you'll miss out on code contract's static checking , other goodies.
Comments
Post a Comment