Skip to content

Fody/PropertyChanging

Repository files navigation

PropertyChanging.Fody

Chat on Gitter NuGet Status

Injects INotifyPropertyChanging code into properties at compile time.

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet installation

Install the PropertyChanging.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package PropertyChanging.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Your Code

[ImplementPropertyChanging]
public class Person
{
    public string GivenNames { get; set; }
    public string FamilyName { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }
}

What gets compiled

public class Person : INotifyPropertyChanging
{
    public event PropertyChangingEventHandler PropertyChanging;

    string givenNames;
    public string GivenNames
    {
        get { return givenNames; }
        set
        {
            if (value != givenNames)
            {
                OnPropertyChanging("GivenNames");
                OnPropertyChanging("FullName");
                givenNames = value;
            }
        }
    }

    string familyName;
    public string FamilyName
    {
        get { return familyName; }
        set 
        {
            if (value != familyName)
            {
                OnPropertyChanging("FamilyName");
                OnPropertyChanging("FullName");
                familyName = value;
            }
        }
    }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }

    public virtual void OnPropertyChanging(string propertyName)
    {
        var propertyChanging = PropertyChanging;
        if (propertyChanging != null)
        {
            propertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }
}

More Info

Icon

Icon courtesy of The Noun Project