The ideal implementation of the INotifyPropertyChanged - Interface

added by DotNetMastermind
11/1/2012 10:43:35 PM

1 Kicks, 275 Views

The ideal implementation of the INotifyPropertyChanged - Interface (GERMAN)


11/23/2012 1:44:25 AM
Your proposed solution is rather complex for a simple problem in my opinion.

Have you heard of the [CallerMemberName] attribute? You can use it to greatly simplify the implementation of INotifyPropertyChanged

Here is an explanation

Dependency Properties do have their place but that's usually to take advantage of some of the advanced data binding and XAML attribute value assigning issues. INotifyProperty is a simple interface. If you use [CallerMemberName] you can implement the minimal implementation that doesn't rely on the developer specifying the property name which can be error prone.

I hope this helps.

11/23/2012 3:50:36 AM

Quite the contrary. I have presented a simple solution to a complex standard situation.

And yes, I know the [CallerMemberName] attribute, which however is only available in C# 5.0 resp .Net Framework 4.5. If you would have taken a closer look at my ViewModelBase class, you would have seen that I have implemented a similar solution for older framework versions:

/// <summary>
/// "Raise" the PropertyChanged event
/// (parameterless => similar to the dotNet Framework 4.5 version)
/// C # 5.0: private void NotifyPropertyChanged([Caller Member name] string propertyName = '')
/// </summary>
protected void RaisePropertyChangedEvent()

In addition, I also have replaced the usual error prone procedure when firing the NotifyPropertyChanged-event with the property name. Instead of firing the event with the property name (as a string) it is now done automatically by corresponding getter and setter. The NotifyPropertyChanged-event will automatically be fired when setting the property over the SetValue(...) method.

I've only used the same method names as you are using with Dependency Properties, so you can already use the familiar in the same way.

So I would strongly suggest that you take a closer look at my ViewModelBase class, then you will realize that I have simplifiy the usage of the INotifyPropertyChanged interface a lot (by the the way, all comments in the ViewModelBase class are written in English).

I hope this helps ...

11/23/2012 9:26:20 PM
I've read the code and I'm simply stating that the [CallerMemberName], albeit only available in C# 5.0, is a lot less code than what you've proposed as the solution.

I've implemented the SetValue, GetValue with Lambda expressions before, however, now that I can use [CallerMemberName], I use it because it's less code and less overhead.

Your solution is OK, albeit a bit long winded, for C# versions < 5.0.