LA.NET [EN]

Nov 07

[Update: I’ve changed the code so that the contract class implements the interface explicitly. This is required for getting static analysis. One more thing: the first release of the project has a bug and  even if you implement the interface explicitly, you won’t get static analysis.]

One of the interest things Code Contracts lets you do is define additional contracts on your interfaces. To achieve this, you need to add a “dumb” class which implements that interface and add the required contracts to the body of the methods. To illustrate the necessary work, lets start by creating a new interface called IPerson:

public interface IPerson {
   String FirstName { get; }
   String LastName { get; }
   void ChangeName(String firstName, String lastName);
}

One of the things I’d like to guarantee is that the method ChangeName will only receive valid names. As we’ve seen in a previous post, that is easily achievable by using the CodeContract.Requires method. Since you cannot define a body on C# interface methods, you’ll need to create a new class which implements the interface and has the restrictions you want to apply:

public class PersonContract:IPerson { 
  string IPerson.FirstName {
            get { return CodeContract.Result<String>(); }
  } 
  string IPerson.LastName {
            get { return CodeContract.Result<String>(); }
  } 
  void IPerson.ChangeName(string firstName, string lastName) {
            CodeContract.Requires(firstName != null);
            CodeContract.Requires(lastName != null);
}
}

It’s important to notice that the contract class must implement the interface explicitly. Since I don’t care about the value returned by the methods on that class, I’ll reuse the values returned by CodeContract.Result<T> method (notice that you can return any valid value from the implementation of the interface in the “contract class”). Now, we need to identity the PersonContract class as the one that defines the contract we want to enforce on our interface. This is achieved with the ContractClassAttribute and ContractClassForAttribute attributes. The first is applied to the interface and the second is applied to the class:

[ContractClass(typeof(PersonContract))]
public interface IPerson    {
   //previous code
}

[ContractClassFor(typeof(IPerson))]
public class PersonContract:IPerson    {
   //previous code
}

And now, you can build the “domain” classes and you’ll automatically inherit the contracts “attached” to the interface. For instance, here’s a valid implementation  for the previous interface:

public class Person:IPerson {
       private String _firstName;
       private String _lastName; 
       public String FirstName {
           get { return _firstName; }
       } 
       public String LastName {
           get { return _lastName; }
       } 
       public void ChangeName(string firstName,string lastName)
       {
           _firstName = firstName;
           _lastName = lastName;
       }
}

Notice how we didn’t add any sort of validation to the class…Simply great,right? On the next post, I’ll keep talking about this great library for applying design by contract on your .NET code. Keep tuned!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>