Tech Tock

Time is of the essence.

Visual Studio INotifyPropertyChanged Snippet

I just made my first Visual Studio snippet.  It’s really easy.

There’s a consensus developing that says dependency properties are usually too heavyweight and people are using INotifyPropertyChanged instead.  I miss the snippet that creates a dependency property, so I made a similar snippet for INotifyPropertyChanged.  You can download the code here.

To make a snippet, just go to Tools / Code Snippets Manager and find a snippet to start with.  I started with the “Define a Dependency Property” snippet since I was doing almost the same thing.  The Snippets Manager will tell you where that file is:

image

Go to the file system and copy the file to your My Code Snippets folder.  If you highlight the folder in the Code Snippet Manager, it will tell you where it is — you can copy the path from the location field.  The file is an xml file, but needs to be named “.snippet”.

Editing the file is very straightforward and the snippet will be available immediately – no restart needed.

One interesting thing about the code is that I’m using a simple method to do all the work:

I basically combined this CodeProject work with Jeff Yates object.Equals idea from here in order to handle both reference and value types in a single call:

Here’s how the snippet comes out by default (the myNames and type are the replaceable fields):

int _myField;

public int MyProperty
{
    get { return _myField; }
    set
    {
        CheckPropertyChanged("MyProperty",
            ref _myField, ref value);
    }
}

This is the method that does a check and notify in the base class:

protected bool CheckPropertyChanged<T>
      (string propertyName, ref T oldValue, ref T newValue)
      {
            if (!Equals(oldValue, newValue))
            {
                oldValue = newValue;
                RaisePropertyChanged(propertyName);
                return true;
            }
            return false;
        }
Advertisements

February 11, 2010 - Posted by | Uncategorized | , , , , ,

3 Comments »

  1. Not sure what heavy means, but MS claims that databinding perf is faster with DPs. And of course they have the potential to consume less memory too

    Comment by Scott Weinstein | February 13, 2010 | Reply

  2. My first instinct would be to use dependency properties, but since like most apps, we’re so far from the limits of the speed and memory that ‘faster’ and ‘less memory’ is not even worth measuring and this stackoverflow post pushes me mildly toward INotify, and its something new to work with, I’m ok with it. These are singleton view models, its pretty hard to go far enough wrong to make a meaningful issue.

    Comment by goldmanalpha | February 15, 2010 | Reply

  3. Some good comments there on SO.

    Here’s what I’ve seen on perf – http://msdn.microsoft.com/en-us/library/bb613546.aspx not huge numbers, but given how easy it is to hit perf issues w/ WPF, I like to get every advantage there is.

    Comment by Scott Weinstein | February 15, 2010 | Reply


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: