posts - 50, comments - 168, trackbacks - 6

My Links

News



View Marcin Celej's profile on LinkedIn

Archives

Post Categories

Design by Contract in .NET

Design by Contract is a methodology that was populated by Eiffel language. The DbC ideas have common aims with Test Driven Development (TDD). Both focus on software quality and tests written with code in parallel.

The idea of DbC is based on contract - contract between client code and service. Each class is a service that provides set of methods (nothing new). Each method call is a client. Contract between them says that if the client satisfies some preconditions then service will ensure some postconditions. That's all. The contracts (or assertions) should be declared easily with the method as there may be lots of them. C# does not support such assertions easily. Some people do it in this way:

public decimal AccountBalance
{
    [Ensures("result > 0")]
    get;
    [Requires("value > 0", "Balance must be greater than zero")]
    [Ensures("AccountBalance == value")]
    set;
}

The Requires attribute represents precondition assertion and the Ensures attribute checks postconditions. The notation is not perfect here but it shows how the assertions (tests) are embedded within code. Let's ignore some performance implications of the methodology and think how it simplifies test-centric development.

Unfortunatelly there is no good implementation of the methodology in .NET yet. There is a research project in Microsoft that tries to introduce such language (Spec#) to the .NET platform. Sample assertions in the Spec# looks like this:

public class ArrayList
{
    public void Insert(int index, object value)
        requires 0 <= index && index < Count
        requires !IsReadOnly otherwise NotSupportedException
    {
        ...
    }
}

As you can see the difference is in syntax only, not in idea of the methodology. So Microsoft goes this way.

I believe the DbC is a powerful mechanism that can save you tears (as many other test-centric mechanisms). I hope the Spec# will become commonly used C# extension (I only played with it yet). Finally: everyone intrested in TDD methodologies should carrefully overview the DbC techniques.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Wednesday, February 15, 2006 6:17 PM | Filed Under [ TDD ]

Feedback

Gravatar

# re: Design by Contract in .NET

What about Eiffel.NET - Eiffel has DBC included in the language itself and I've heard it's really good. So when you say there are no good implementations for .NET - is this included? Eiffel.NET runs within VS.NET 2k3 and 2k5 I believe. I don't have the website but if you google it you should have no problem finding the homepage for Eiffel.NET.
4/27/2006 4:40 PM | troy
Gravatar

# re: Design by Contract in .NET

Sorry, I should have thought before I responded.... the link for Eiffel that you provide IS the maker of Eiffel.NET as well... and for clarification, it is not yet available (officially) for VS.NET 2k5
4/27/2006 4:50 PM | troy
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: