I have an MVVM project in C # and I want to use code contracts in it. So this is my scenario: Interface:
public interface IC042_Model
{
void Save(C042 entity);
void Delete(C042 entity);
}
Then I have an abstract class for contracts:
[ContractClassFor(typeof(IC042_Model))]
internal abstract class C042_Model_Contracts : IC042_Model
{
public void Save(C042 entity)
{
Contract.Requires(entity != null);
}
public void Delete(C042_CondicaoPagamento entity)
{
Contract.Requires(entity != null);
}
}
In another project, my model implements an interface, and if I call this.Save (zero), a warning is generated in any method. In my ViewModel, if I call the same method above: this.Save (null), no warning is generated, but when I run the application, the above line throws a Contract exception.
Is there something wrong with my approach?
Thanks in advance.
I made another example, which, I think, will be easier for everyone to understand:
I created the following class in a class library project:
public static class StringExtensions
{
public static string TrimAfter(string value, string suffix)
{
Contract.Requires(suffix != (string)null);
Contract.Requires(!string.IsNullOrEmpty(suffix));
Contract.Requires(value != null);
var index = value.IndexOf(suffix);
if (index < 0)
return value;
return value.Substring(0, index);
}
}
When I call it from a WPF project, as shown below:
CodeDigging.StringExtensions.TrimAfter(null, null);
I do not receive a warning that the contracts are not a complete field.
, , .
.