I have moved!

I've moved my blog

Friday, 22 August 2008

Observable Property Pattern

Is there something like this in the standard libraries? And if not, why not?

public class Observable<T>
    private T _value;

    public class ChangedEventArgs : EventArgs
        public T OldValue { get; set; }
        public T NewValue { get; set; }

    public EventHandler<ChangedEventArgs> Changed;

    public T Value
        get { return _value; }

            if (!value.Equals(_value))
                T oldValue = _value;
                _value = value;

                EventHandler<ChangedEventArgs> handler = Changed;
                if (handler != null)
                    handler(this, new ChangedEventArgs
                                      OldValue = oldValue, 
                                      NewValue = value

That is, the observable property pattern as a reusable class.

This should be in the System namespace for all to enjoy. It's a great thing when what used to be a "pattern" can now be captured in code using language features (the enabling feature in this case being generics, obviously).

1 comment:

Marcel said...

Hmm. Nice idea. You're right, it should be in the standard libraries.