Question

We want to use code contracts on our interfaces. We have a large interface hierarchy, leading to an awful lot of duplication in the contract classes.

We have an interface IX which (through inheriting from other interfaces) has around 50 methods. There are several interfaces that derive from IX. Interface IY inherits from IX, adding only a couple of methods. If I want to add a contract class for IY, I need to reference all 50+ methods. Worse, if later on I decide to modify IX, I have to change all the contract classes for all interfaces inheriting from IX. This discourages developers from making changes.

Is there a way of avoiding this duplication?

Was it helpful?

Solution

I don't believe this is possible, no. You can't derive one interface contract class from another, and interface contracts classes can't have any base class other than System.Object. So I think you're stuck. Your best way forwards would probably involve some kind of code generation tool, or lots of copy-and-pasting.

OTHER TIPS

I have this same problem with some code I have been writing although not quite on the same scale. My solution has been to use the IDE/ReSharper to just add the NotImplementedException stubs for new methods or write them as abstract override. All contracts defined in the base class should be in stone at that point as the CC tools will yell at you if you attempt to add Contract.Requires to them in the contract class for IY for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top