Refactoring friendly INotifyPropertyChanged

Oct 21, 2009 at 9:25 AM

Could something like http://blog.m.jedynak.pl/2009/02/static-typed-propety-names.html be considered to make the handling of INotifyPropertyChanged a bit more refactor friendly compared to const strings.

Coordinator
Oct 27, 2009 at 8:17 PM

Hi,

Thanks a lot for your comment. I considered many possibilities to avoid working with "magic strings" but my personal opinion is that using other alternatives (such as the one proposed in the link you mention) are rather complex and "frightening" for unexperienced programmers, who are a large target for the MVVM Light Toolkit. I will however add the following in V3:

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090051

This solution developed by Josh Smith (who is already a contributor to the MVVM Light Toolkit through the RelayCommand that he very nicely agreed to let me use) checks in DEBUG mode that a given property name actually exists on the calling object. This is, I think, a satisfying compromise. On one hand we keep the simplicity of using strings instead of harder-to-understand expressions, and on the other hand we get an error if a property name gets changed and some spots are missed where it is used.

Note that if you use the constant consistently throughout the code, you should be fine when you change a property name. The name itself is defined as a string only once, and all other references are checked at compilation.

Let me know if you think this is not good enough.

Thanks,

Laurent

Coordinator
Apr 18, 2010 at 6:51 PM

Hi,

As you are a previous user of the discussion tab on the MVVM Light Codeplex site, I would like to inform you that I decided to promote the usage of StackOverflow for questions regarding the MVVM Light toolkit. Please tag your questions with the mvvm-light tag.

StackOverflow is an awesome site where tons of developers help others with their technical question.

http://stackoverflow.com/questions/tagged/mvvm-light

I will monitor this tag on the StackOverflow website and do my best to answer questions. The advantage of StackOverflow over the Codeplex discussion is the sheer number of qualified developers able to help you with your questions, the visibility of the question itself, and the whole StackOverflow infrastructure (reputation, up- or down-vote, comments, etc)

Thanks!

Laurent

May 14, 2010 at 10:50 AM

Hi, I too also think this would be a good idea. If you feel its frightening for inexperienced programmers then why not a simple overload, best of both worlds?

In the meantime I am using this:

 

    public class ViewModelBaseExt : ViewModelBase
    {
        public void RaisePropertyChanged<TProperty>(Expression<Func<TProperty>> propertyExpression)
        {
            RaisePropertyChanged(GetMemberInfoName(propertyExpression));
        }

        private static string GetMemberInfoName(Expression expression)
        {
            var lambda = (LambdaExpression)expression;

            MemberExpression memberExpression;
            if (lambda.Body is UnaryExpression)
            {
                var unaryExpression = (UnaryExpression)lambda.Body;
                memberExpression = (MemberExpression)unaryExpression.Operand;
            }
            else memberExpression = (MemberExpression)lambda.Body;

            return memberExpression.Member.Name;
        }
    }