Use case: I'm using data templates to match a View to a ViewModel. Data templates work by inspecting the most derived type of the concrete type provided, and they don't look at what interfaces it provides, so I have to do this without interfaces.
I'm simplifying the example here and leaving out NotifyPropertyChanged, etc., but in the real world, a View is going to bind to the Text property. For simplicity, imagine that a View with a TextBlock would bind to a ReadOnlyText and a View with a TextBox would bind to WritableText.
class ReadOnlyText
{
private string text = string.Empty;
public string Text
{
get { return text; }
set
{
OnTextSet(value);
}
}
protected virtual void OnTextSet(string value)
{
throw new InvalidOperationException("Text is readonly.");
}
protected void SetText(string value)
{
text = value;
// in reality we'd NotifyPropertyChanged in here
}
}
class WritableText : ReadOnlyText
{
protected override void OnTextSet(string value)
{
// call out to business logic here, validation, etc.
SetText(value);
}
}
By overriding OnTextSet and changing its behavior, am I violating the LSP? If so, what's a better way to do it?