INotifyPropertyChanged is chiseled into the brain of every XAML developer. It provides amazing data binding power, but it has one drawback for me - code bloat. Take for example the code below, it's a class with 3 properties: LastName, FirstName and Age. It's a pretty simple class but there is a lot of framework code detracting away from the simplicity.

public class PersonWithoutFody : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private int _age; private string _firstName; private string _lastName; public string LastName { get { return _lastName; } set { _lastName = value; RaisePropertyChanged(); } } public string FirstName { get { return _firstName; } set { _firstName = value; RaisePropertyChanged(); } } public int Age { get { return _age; } set { _age = value; RaisePropertyChanged(); } } private void RaisePropertyChanged([CallerMemberName]string propertyName = "") { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }

What I would love is a simpler version which keeps my code true to what it was originally. That is where Fody comes in. The same code with Fody looks like this:

public class PersonWithFody : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public int Age { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }

The lines of code reduce dramatically and is clean from framework code.

Under the hood Fody weaves the equivalence of what you see in the original file at compile time.

Setting up Fody

Let's talk about how to set this up. There are 3 simple steps.

1.Nuget

The first step is to add the nuget package.

2.FodyWeavers.xml

Next up, you'll need to create a FodyWeavers.xml file and add it to your project. This helps link the code weaving to the compiler.