Design by contract is
an approach to programming where one establishes pre and post conditions when calling
methods or modules. The idea is that by asserting that by defining and validating
the interface in this manner bugs are reduced and are easier to find.
I found a framework for doing this called eXtensible
C# (XC#) which allows for pre and post conditions using attributes. They
use a compiler of sorts to do compilation after the C# compiler for you code has been
invoked from VS.Net. Here is an example of how one would use XC#:
[Requires ("app != null")]
void UpdateApplication (Application app)
XC# comes as an add-in to VS.Net that appears to hook into the post build event.
You can also add obfuscation to your project by simply adding an Obfuscate attribute
to the assembly.
Some of the alternatives to this approach are:
-
Using Aspect Oriented
Programming (AOP) to automatically intercept the calls. One should be aware
that this is most likely quite slow. Here are some example open source AOP frameworks
for .Net:
-
Manually coding the rules at the beginning or the end of each method call. There
is a 2002 CodeProject Design
by Contract article about doing this.
-
Using a home brewed attribute validation system [which is really what XC# is doing
in any case]