Help |
Background |
Implementation |
Example |
Feedback
Welcome to ContractN: A managed programming-by-contract framework.
ContractN provides a simple, yet highly effective and flexible, programming-by-contract library for managed APIs. It uses the CLR's support for aspect-oriented programming (AOP), enabling seamless integration into existing code.
In layman’s terms, ContractN allows you to adorn APIs with attributes such as, InRequired, OutRequired, ReturnRequired, PreCondition, PostCondition and even custom conditions, with various points of extensibility.
Help
Conceptual and API reference documentation will be published for the next beta release.
Background
This project was published on Monday, March 10, 2008 by
Dave Sexton.
The source code is written in C# 3.0 and consists of a single Visual Studio 2008 project. The assembly that it produces targets the Microsoft .NET Framework 2.0.
The concept for this library was derived from an idea that was published by
Sasha Goldshtein in
Interception and Attributes: A Design-By-Contract Sample, although the ContractN implementation is entirely original; i.e., no implementation details were taken.
People have also been discussing whether first-class support for DbC in C# 4.0 would be a worthwhile feature (e.g., see a blog post by John Skeet:
C# 4, part 2: Ideas from other community members).
If you're not familiar with the concept of programming-by-contract, check out
this Wikipedia article for an overview and related links. Then after trying out this framework,
ask the C# team to support DbC in a future release if you think it could be useful.
Implementation
ContractN relies on the CLR's support for
aspect-oriented programming (AOP), so it integrates seamlessly with the rest of your code, although with one catch: participating objects must derive from a specific base class.
For more information about managed AOP, see the following MSDN article:
Aspect-Oriented Programming Enables Better Code Encapsulation and Reuse.
Note that the CLR's implementation of AOP uses the remoting infrastructure, so if ultra-high performance is a concern then you may want to consider using a different tool.
Example
Here's an example business object in C# 3.0 that illustrates how to use ContractN on the most
basic level. Note that there are other features that are not shown here.
1. Define a programming-by-contract class and add condition attributes in various places.
[PreCondition("NotDisposed")]
public class Person : ContractN.ProgrammingByContract, IDisposable
{
[InRequired, OutRequired]
public string Name { get; set; }
private bool disposed;
private void NotDisposed()
{
if (disposed)
throw new ObjectDisposedException(this.GetType().FullName);
}
public void Dispose()
{
disposed = true;
}
}
2. Use the class like normal.
Person person = new Person();
person.Name = null; // throws ArgumentNullException
string name = person.Name; // throws ContractN.ReturnNullException
person.Name = "DbC";
person.Dispose();
name = person.Name; // throws ObjectDisposedException
Feedback
Please use the
Discussions area and
Issue Tracker here on CodePlex to provide feedback. For private matters please contact
Dave Sexton.