I'm trying to do a Interface for property changes in a product. In some case the values are int others the values would by string. The new value is required, but sometimes the field don't have a old value.
Something like this:
public interface IChangeSuggestion<TValue> {
TValue? OldValue { get; set; } //Line with error
TValue NewValue { get; set; }
}
public class ChangeSuggestionSomeId : IChangeSuggestion<int> {
public int? OldValue { get; set; }
public int NewValue { get; set; }
}
public class ChangeSuggestionName : IChangeSuggestion<string> {
public string OldValue { get; set; }
public string NewValue { get; set; }
}
the code above throw the error: A nullable type parameter must be known to be a value type or non-nullable reference type. Consider adding a 'class', 'struct', or type constraint.
Edit
I'm using the interface because a shared approval logic, and the classes are Models for the Entity Framework
To make work in the Entity Framework the follow code do the trick:
public interface IChangeSuggestion<TValue> {
TValue OldValue { get; set; }
[Required]
TValue NewValue { get; set; }
int Id { get; set; }
int ProductId { get; set; }
bool? Approved { get; set; }
DateTime? ApprovedOn { get; set; }
string ApprovedById { get; set; }
}
public class ChangeSuggestionSomeId : IChangeSuggestion<int?> {
public int? OldValue { get; set; }
[Required]
public int? NewValue { get; set; }
public int Id { get; set; }
public int ProductId { get; set; }
public bool? Approved { get; set; }
public DateTime? ApprovedOn { get; set; }
public string ApprovedById { get; set; }
}
This works because i'm using Entity Framework that run a validate on SaveChanges, but i think that strictly NewValue should be a int not a int?