Have you ever been annoyed with having to implement the cumbersome plumbing required for INotifyPropertyChanged ? Well, I have. So I tried to find a way to make authoring bindable objects better.

The typical example:

As you can see, it is quite verbose. The event and OnPropertyChanged() method need to be implemented for each class and it’s easy to get the implementation of OnPropertyChanged() wrong (typically introducing a race condition). Moreover, it’s 10 lines of code for each property. I hate that. The more you have to write, the bigger a surface for bugs to appear. A property should just boil down to   public string FirstName { get; set; } .

That won’t be possible of course. The default setters and getters just handle the assignment and loading of a compiler-generated backing field. So we somehow need to add the code for each property.

Enter Bindable objects

Similar to class NotificationObject, Bindable is here to help implementing INotifyPropertyChanged. The NotificationObject class only implements RaisePropertyChanged() but does not help with the implementation of the properties. Here is what you can do with Bindable:

Noticeably shorter, isn’t it ?

Behind the scene, Bindable uses a dictionary to store the property values. You probably have noticed that the property name is not given to Bindable.Get() or Bindable.Set(). Bindable leverages the compiler to provide the value automatically:

CallerMemberNameAttribute when applied on an optional parameter instructs the compiler to pass a string whose value is the name of the calling member. So when property FirstName calls Get<string>() , the compiler generates code for  Get<string>("FirstName") .

Here is the actual code for class Bindable:

Some aspects can be improved. For example, enabling subclasses to provide their own backing field. This is left as an exercise to the reader ;-)

I use this class a lot when implementing MVVM either in Wpf or Winforms.

What do you think ?

Edit: fixed code snippets as per Krumelur’s comments. Thanks Krumelur.

3 thoughts on “Annoyed with INotifyPropertyChanged?

  1. B-R-I-L-L-I-A-N-T idea! However: I cannot get it to build :-( I tried VS2012 and Xamarin Studio.

    In the Set I’m getting:

    Error 1 The best overloaded method match for ‘Set(string, string)’ has some invalid arguments
    Error 2 Argument 2: cannot convert from ‘T’ to ‘string’

    Reply
    • Oh, just noticed: you’re missing code. That’s causing the build error. Set should probably be:

      protected void Set(T value, [CallerMemberName] string name = null) {
      Debug.Assert(name != null, “name != null”);
      if (Equals (value, Get (name)))
      {
      return;
      }
      _properties[name] = value;
      OnPropertyChanged(name);
      }

      Reply
      • Krumelur, you are right. Apparently, I used a version of the code before I refactored it. I hit the same issue myself.
        Btw, sorry for the late reply, I was enjoying some hollidays :-)

        Reply

Leave a Reply