tags:

views:

155

answers:

4

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?

+7  A: 

Only if the specification of ReadOnlyText.OnTextSet() promises to throw.

Imagine code like this

public void F(ReadOnlyText t, string value)
{
    t->OnTextSet(value);
}

Does it make sense to you if this didn't throw? If not, then WritableText isn't substitutable.

It looks to me like WritableText should inherit from Text. If there's some shared code between ReadOnlyText and WritableText, put it in Text or in another class that they both inherit from (that inherits from Text)

Lou Franco
You are correct based on my specific question, but Brandon pointed out a misconception I had about property setters and getters that allowed me to solve the problem more elegantly. Thanks for the info.
Scott Whitlock
+2  A: 

I'd say depends on the contract.

If the contract for ReadOnlyText says "any attempt to set Text will throw an exception", you are certainly violating LSP.

If not, you still have an awkwardness in your code: a setter for a read only text.

That is an acceptable "denormalization" under given circumstances. I've not yet found a better way that doesn't rely on lots of code. a Clean interface would be in most cases:

IThingieReader
{
    string Text { get; }
    string Subtext { get; }
    // ...
}

IThingieWriter
{
    string Text { get; set; }
    string Subtext { get; set; }
    // ...
}

...and implementing the interfaces only when appropriate. However, that breaks down if you have to deal with instances where e.g. Text is writable and Subtext is not, and is a pain to do for many objects / properties.

peterchen
As I said, this would be ideal, but I can't use interfaces because Data Templates won't key off an interface, they need a concrete type.
Scott Whitlock
A: 

Yes it does, it wouldn't if the protected override void OnTextSet(string value) also threw an exception that was of type "InvalidOperationException" or inherited from it.

You should have a base class Text and both ReadOnlyText and WritableText inheriting from it.

ThanosPapathanasiou
+5  A: 

LSP states that a subclass should be substiutable for it's superclass (see stackoverflow question here). The question to ask yourself is, "Is writeable text a type of readonly text?" The answer is clearly "no", in fact these are mutually exclusive. So, yes, this code violates LSP. However, is writable text a type of readable text (not readonly text)? The answer is "yes". So I think the answer is to look at what it is you're trying to do in each case and possibly to change the abstraction a bit as follows:

class ReadableText
{
    private string text = string.Empty;
    public ReadableText(string value)
    {
        text = value;
    }

    public string Text
    {
        get { return text; }
    }
}          

class WriteableText : ReadableText
{
    public WriteableText(string value):base(value)
    {

    }

    public new string Text
    {
        set
        {
            OnTextSet(value);
        }
        get
        {
            return base.Text;
        }
    }
    public void SetText(string value)
    {
        Text = value;
        // in reality we'd NotifyPropertyChanged in here       
    }
    public void OnTextSet(string value)
    {
        // call out to business logic here, validation, etc.       
        SetText(value);
    }
}     

Just to be clear, we're hiding the Text property from the Readable class using the new keyword on the Text property in the Writeable class.
From http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx: When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class.

Brandon
This doesn't quite compile, but can be modified to work. I had no idea you could define a property setter in a derived class for a property with a getter in a base class. I learned something new. This will work. Thanks!
Scott Whitlock
Interestingly, I need to declare the Text property in the derived class as "new" to avoid the compiler warning, but in my opinion it's not really new because I'm not overriding the getter.
Scott Whitlock
I do not believe you want SetText and OnTextSet to be public
Steve Ellinger
I edited the post to include the new keyword. C# is telling us we're hiding the Text property in the Readable class because the property is only readable (getter), but in the Writeable class the property is readable (getter) and writeable (setter).
Brandon
@Steve Ellinger: True, in my implementation the base class handles updating the property and notifying the View when the business logic changes the value.
Scott Whitlock