INotifyPropertyChanged

The thing that bothers me the most about MVVM and similar patterns is the ugly code that you have to write just to implement INotifyPropertyChanged interface on you view models. So instead of my nice and shiny class “A” I need to write the horrible monster “B”.

// exhibit "A"
class Sale
{
    public string Name { get; set; }
    public Decimal Price { get; set; }
    public Decimal Quantity { get; set; }
    public Decimal Total { get { return Price * Quantity; } }
}

// exhibit "B"
class Sale : INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }

    private Decimal price;
    public Decimal Price
    {
        get { return price; }
        set
        {
            if (price != value)
            {
                price = value;
                NotifyPropertyChanged("Price");
                NotifyPropertyChanged("Total");
            }
        }
    }

    private Decimal quantity;
    public Decimal Quantity
    {
        get { return quantity; }
        set
        {
            if (quantity != value)
            {
                quantity = value;
                NotifyPropertyChanged("Quantity");
                NotifyPropertyChanged("Total");
            }
        }
    }

    public Decimal Total { get { return Price * Quantity; } }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Everybody can see what the problem with “B” is. Not only the code is ten times longer but it does not mean or do anything particularly fancy. The code that I can live with looks something like this:

// exhibit "C"
class Sale : BaseClass
{
    [NotifyPropertyChanged]
    public string Name { get; set; }

    [NotifyPropertyChanged("Price", "Total")]
    public Decimal Price { get; set; }

    [NotifyPropertyChanged("Quantity", "Total")]
    public Decimal Quantity { get; set; }

    public Decimal Total { get { return Price * Quanitity; } }
}

As of this moment I do not know how to make “C” happen. My guess would be that we either need some (pre-) compiler magic or something like an aspect (as in AOP). I am willing to bet $20 that something like this will either happen in one of the MVVM frameworks very soon (and that will be my framework of choice) or Microsoft will implement it in the next version of C#.

One thought on “INotifyPropertyChanged

  1. Example C will require IL weaving. I think you may be able to achieve this most easily if Compiler as a Service is realized. Right now, your options are to build your own using System.Reflection.Emit or use one of the libraries out there: Spring.NET, PostSharp, or Castle DynamicProxy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s