Learning more about code contracts, they seem like something I want to use in my project.
Given the way that I'm structuring my web-service-layer (an abstraction of our service layer accessed from our MVC controllers) I find myself wondering why it's not possible to specify different contracts for the various permutations of an interface implementation.
Specifically I'm interested in the reasons behind only being able to specify the contracts in a 1-1 fashion given a generic interface method.
Here's an example of my code structure and I'll aim to identify how I'd like to use code contracts. I'm sure someone with more experience will be able to nudge me in the right direction.
I'm using a CQRS style approach such that:
public interface IQuery<in TInput input,out TOutput output>
{
TOutput Invoke(TInput request)
}
public interface IGetSomeUnicornsFromAMagicalLand :
IQuery<int, IEnumerable<Unicorn>>{}
// Implementation
public class GetSomeUnicornsFromMagicLand : IGetSomeUnicornsFromAMagicalLand
{
public IEnumerable<Unicorn> Invoke(int numberOfUnicornsToReturn)
{
// Here I'd like to specify some preconditions on the input,
// specific to type int
return _wizardry
.GetMagicCreature<Unicorn>(numberOfUnicornsToReturn)
.DoMagicalConversionToEnumerable()
}
}
Given this context it seems reasonable to want to specify contracts at the implementation level rather than in an abstract class designed to apply contracts on the interface (as a generalised mechanism).
- What are some of the reasons for not being able to do this?
- Are there other approaches that cater for this?
- Is this just not a good structure if I want to use code contracts?