Saturday, January 30, 2010

A C# Property class

Soon after starting work in C# there were a couple of code patterns that arose that made me miss the good ol' C/C++ pre-processor. One of these is the basic pattern of a class property with change notification.

You've got a class and you want the state of its properties to be observable via events. How many time have you written the following bit of code in some for or other?
int _age;

public event EventHandler AgeChanged;

protected virtual void OnAgeChanged()
{
  if(Changed != null)
    Changed(this, EventArgs.Empty);
}

public int Age
{
  get{ return _age; }
  set
  {
    if(value != age)
    {
      value = age;
      OnAgeChanged();
    }
  }
}
Automatic properties help when you don't need notification, INotifyPropertyChange abstracts that event a little. Still, a lot of boilerplate. Having written a fair amount of MFC in the years prior I really wanted to be able to do something like:
DECLARE_PROPERTY(Age, int)
and have a fancy macro that would handle the boiler plate. Alas there is no macro pre-processor in C#-land.

The introduction of .NET generics opens up the opportunity to model a class property as a class itself, while retaining type safety and the same assignment semantics as intrinsic types. So this little class (and a derived class with a cancellable event) has become a handy part of my toolbox.

public class Property<T>
{
  protected T _value = default(T);

  public Property()
  {
  }

  public Property(T value)
  {
    _value = value;
  }

  public event EventHandler Changed;

  public virtual T Value
  {
    get { return _value; }
    set
    {
       if ((value != null && !value.Equals(_value)) ||
          (_value != null && !_value.Equals(value)))
       {
          _value = value;
          OnChanged();
       }
    }
  }

  protected virtual void OnChanged()
  {
     if (Changed != null)
       Changed(this, EventArgs.Empty);
  }

  public static implicit operator T(Property property)
  {
    if (property == null)
      return default(T);

    return property.Value;
  }
}
using it is just a matter of declaring a public field on a class declaration.
class Person
{
  public readonly Property<int> Age = new Property<int>();

  public readonly Property<string> Name = new Property<string>();
}
Interacting with the properties looks a lot like an intrinsic:
Person don = new Person();
don.Name.Value = "Don";
don.Age.Value = 41;

if (don.Age > 40)
  Console.WriteLine("Ooooooold");
 
with the difference that every property now comes with a built in Changed event
don.Age.Changed += new EventHandler(Age_Changed);

So anyway, I've found this to be a handy helper class and thought perhaps you might as well.

PS: the cancellable derived class looks like this:
class CancellableProperty<T> : Property<T>
{
  public CancellableProperty(T value)
    : base(value)
  {
  }

  public event CancelEventHandler Changing;

  public override T Value
  {
    set
    {
      if ((value != null && !value.Equals(_value)) ||
         (_value != null && !_value.Equals(value)))
      {
        CancelEventArgs args = new CancelEventArgs(false);
        OnChanging(args);
        if (args.Cancel == false)
        {
          _value = value;
          OnChanged();
        }
      }
    }
  }

  protected virtual void OnChanging(CancelEventArgs args)
  {
    if (Changing != null)
      Changing(this, args);
  }
}