Eden Ridgway's Blog

.Net and Web Development Information

  Home :: Contact :: Syndication  :: Login
  105 Posts :: 1 Stories :: 78 Comments :: 3 Trackbacks

Search

Article Categories

Archives

Post Categories

Development

General

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:

  1. 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:
  2. 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.
  3. Using a home brewed attribute validation system [which is really what XC# is doing in any case]
posted on Saturday, September 24, 2005 2:35 AM
Comments have been closed on this topic.