ユース ケース: データ テンプレートを使用して、View を ViewModel に一致させています。データ テンプレートは、提供された具象型の最も派生した型を検査することで機能し、それが提供するインターフェイスを調べないため、インターフェイスなしでこれを行う必要があります。
ここでは例を簡略化し、NotifyPropertyChanged などを除外していますが、現実の世界では、View は Text プロパティにバインドされます。簡単にするために、TextBlock を持つ View は ReadOnlyText にバインドし、TextBox を持つ View は 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);
}
}
OnTextSet をオーバーライドしてその動作を変更すると、LSPに違反しますか? もしそうなら、それを行うためのより良い方法は何ですか?