My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

PropFu

PropFu is a plugin for PostSharp that eliminates the mind numbing and somewhat risky repetition of declaring properties that support INotifyPropertyChanged. If you work with WPF, or WinForms you know how painful this can be. With this simple plugin to the PostSharp aspect library all of your properties can magically be turned into automatic properties without a backing field, and all of your properties will call a method of your choosing to fire the PropertyChanged event. All you need to do is add one attribute to the top of your class, and one attribute to the method that will fire the event (usually in a thin, common base class) and you're done! Everything is done at compile time and you will receive absolutely no runtime performance hit. Once you start using this technique you'll never go back.

Note: This can be used in commercial/closed source software as the only code you will be linking to directly in your project is under the MIT license. The plug-in is GPL, but that does not transfer over to an app modified by the plugin:

http://www.postsharp.org/forum/post1416.html#p1416

Here's what you had to do before:

    public class PersonLame : INotifyPropertyChanged
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if (value == _name)
                    return;

                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private int _age;
        public int Age
        {
            get { return _age; }
            set
            {
                if (value == _age)
                    return;

                _age = value;
                OnPropertyChanged("Age");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged == null)
                return;

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Here's the same as above using PropFu:

    [NotifyPropertyChanged]
    public class PersonAwesome : INotifyPropertyChanged
    {
        public string Name { get; set; }

        public int Age { get; set; }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        [OnPropertyChanged]
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged == null)
                return;

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

How it works: You simply mark classes with the NotifyPropertyChanged attribute, when you want to have properties that fire the property changed event. You mark a method, anywhere in your class hierarchy with the OnPropertyChangedAttribute, and that method will be called when the property value is changed. The system even injects a guard clause that checks to see if the value has changed.

Notes:

  • This works even when you do not use automatic properties.
  • Only properties with public getters, and any kind of setter will fire property changes.
  • Guard clauses are injected in your setters, so if the value being sent in is the same, the code in your setter will not get executed. (You shouldn't be doing anything in your setter that changes the state of your object when the same value is passed in anyway.)

Installation

When PostSharp is installed

Copy files PropFu.psplugin, PropFu.Weaver.dll, and PropFu.dll into C:\Program Files\PostSharp 1.0\PlugIns (for global installation) or C:\Documents and Settings\userName\Application Data\PostSharp 1.0 (user-only installation).

You need to link to the PropFu.dll in your code to get access to the attributes.

When you are using only the binaries of PostSharp

If you are going to have PropFu, and PostSharp checked into your source control system, and do not wish to make all your developers/build machines install PostSharp, you need to do a bit of manual work per project.

To inject aspects into an assembly, PostSharp waits for the project to compile, and then during post-build reads in the assemblies and starts weaving code at the IL level. To make this happen, you need to do the following:

  1. Add a reference to PostSharp.Public in your project. For example sake, let's say you have your binaries extracted to: C:\PostSharp
  2. Open the .csproj that will be using aspects in notepad.
  3. Locate this line near the bottom of the file: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  4. You need to put a property group before the above line that tells PostSharp not to link in the PostSharp.Public or PostSharp.Laos DLLs into your assembly, and import the PostSharp.targets file. Only use relative paths when pointing to the targets file in order to work correctly with source control. You basically sandwich the above line and end up with this:
<PropertyGroup>
   <DontImportPostSharp>True</DontImportPostSharp>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\..\PostSharp\PostSharp.targets"/>

You probably need to change the relative path in the above snippet, depending on where your project is located relative to PostSharp directory.

Powered by Google Project Hosting