PropertyChangedReactions 1.0.2

dotnet add package PropertyChangedReactions --version 1.0.2
NuGet\Install-Package PropertyChangedReactions -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PropertyChangedReactions" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PropertyChangedReactions --version 1.0.2
#r "nuget: PropertyChangedReactions, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PropertyChangedReactions as a Cake Addin
#addin nuget:?package=PropertyChangedReactions&version=1.0.2

// Install PropertyChangedReactions as a Cake Tool
#tool nuget:?package=PropertyChangedReactions&version=1.0.2

README

Small helper project to handle property changes and conditionally execute post change behaviors. This helps remove the boilerplate of backing fields and allows for defining behaviors to run after setting a property.

Background

Sometimes logic needs to execute after a property changed and that typically requires a backing field to do something like

public int SomeProperty
{
    get
    {
        return _someBackingField;
    }
    set
    {
        if(_someBackingField == value)
            return;
        _someBackingField = value;

        SomeLogic();

        if (foo)
            SomeOtherLogic();
    }
}

This project takes that idea and attempts to wrap it up in a cleaner way.

Example

Basic get/set

For basic getting and setting, you can use the property model to handle the boiler plate for you, without backing fields. It also cleans up the code a bit, for example updating a property type would just be changing the type definition in the Get method.

public int IntProperty
{
    get => Model.Get<int>();
    set => Model.Set(value);
}

Set Only If Value Changed

To filter out the "if not changed return" boilerplate, the SetIfChanged can be used.

 public bool IsSomeState
{
    get => _model.Get<bool>();
    private set => _model.SetIfChanged(value);
}

Note: This is the default behavior when using PropertyChangedReaction

Adding Post OnChanged Behaviors

To run logic after a property is changed, PropertyChangedReaction objects can be used.

public string StringProperty
{
    get => _model.Get<string>();
    set => _model.Reaction()
        .AddOnChangedBehavior(() => Foo())
        .AddOnChangedBehavior(() =>
        {
            if (bar)
                Baz();
        })
        .Set(value);
}

Even Cleaner Version

The above is to demonstrate the idea, but still has boilerplate in the set logic. This can of course be reduced with some refactoring.

public string StringProperty
{
    get => _model.Get<string>();
    set => _model.Reaction()
            .AddOnChangedBehavior(OnStringPropertyUpdated)
            .Set(value);
}
private void OnStringPropertyUpdated()
{
    Foo();

    if(bar)
        Baz();
}

Or

// set example
set => UpdateStringProperty(value);
UpdateStringProperty(string newVal)
{
    _model.Reaction(nameof(StringProperty))
        .AddOnChangedBehavior(OnStringPropertyUpdated)
        .Set(value);
}

The above snippets add 2 behaviors to run after the property StringProperty is changed to a new value. After the set, and the native OnPropertyChanged executes, the 2 additional behaviors will run.

This works by first calling the Reaction method which starts a reaction chain, allowing the setup of the behaviors. At the end is the call to Set() which commits the property value, updates if changed (again the reaction workflow does not do direct value setting, and only sets if different) and fires the post-changed behaviors.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 222 3/16/2023
1.0.1 204 3/13/2023
1.0.0 212 3/13/2023

changed to target .net standard 2.0 from 2.1