I want to create a code contract for a certain interface, however I'm having a hard time believing that it is actually done this way.
[ContractClass(typeof(AsyncCacheProviderContract))]
public interface IAsyncCacheProvider {
Task<bool> GetAsync<T>(string key, out T value);
}
[ContractClassFor(typeof(AsyncCacheProviderContract))]
internal abstract class AsyncCacheProviderContract : IAsyncCacheProvider {
public Task<bool> GetAsync<T>(string key, out T value)
{
Contract.Requires(!String.IsNullOrEmpty(key));
value = default(T);
return Task.Factory.StartNew(() => false);
}
}
The contract should ensure that 1) all classes implementing the interface require the argument key to be not null or empty as well as to 2) auto-generate the check into the build, e.g. similar to
public Task<bool> GetAsync<T>(string key, out T value) {
if(String.IsNullOrEmpty(key))
throw new ArgumentException //...
}
However in this particular case, it feels strange to me that I have to assign the out
argument as well as return a dummy Task
just to make the compiler happy. Is there no more straightforward way, e.g. using attributes?