Rx.Net.Plus 1.1.4

Rx.Net variables library. Provide reactiveness to variables. See readme for more details

There is a newer version of this package available.
See the version list below for details.
Install-Package Rx.Net.Plus -Version 1.1.4
dotnet add package Rx.Net.Plus --version 1.1.4
<PackageReference Include="Rx.Net.Plus" Version="1.1.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rx.Net.Plus --version 1.1.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Rx.Net.Plus

Bring reactiveness to variables and properties

ReactiveX gains popularity and is widely used in several platforms and languages.

If you have used Rx.Net (System.Reactive), you probably already dreamed to transform regular variable (like int, bool ) to become reactive.

What do we mean?

We would like to:

  1. React whenever variables change
  2. Connect variables to observable sources
  3. Not receive notification if same data is set again and again
  4. Continue to use these variables as before. Compare them to values, convert them...

Rx.Net.Plus introduces two new types for this purpose (+ several extension methods):

| Type | Purpose |
| :--------- | ------------------------------------------------------------ |
| RxVar | replace regular variables (like int, bool...) |
| RxProperty | to replace view-model properties (NotifyPropertyChanged pattern) |

RvVar

Let start with an example:

 var isConnected = false.ToRx(); // Create a reactive variable`

Let say, connectionStatus is of type Observable

Observable connectionStatus;

we can do:

connectionStatus.Subscribe (isConnected);

or using our method:

isConnected.ListenTo (connectionStatus);

Now, RxVar's magic is to allow the following syntax:

if (isConnected != true)
{
	// Do something.
}

As well:

var anotherRxBool = true.ToRx();

if (isConnected == connectionStatus)
{
	// Do some action
}

With numeric variables (int, double...)

var intRx = 10.ToRx();               // Create a rxvar

if (intRx > 5)
{
	// React !!
}

RxVar could be used as part of state machine, and instead of polling statuses, flags, counters...

Just react upon change in this manner:

isConnected.When(true).Notify (v => reactToEvent());

RxVar implements the following interfaces:

ISubject, IConvertible, IDisposable, IComparable, IEquatable

Distinct mode

By default, RxVar propagates its data (on update) only when a new distinct value is set.

That means that if some observer is subscribed to RxVar and the same value is assigned to RxVar, it will not be published.

In order to allow every update to be propagated, RxVar provide the following method:

SetDistinctMode

Limitations

As C# does not support overloading of the assignment operator, it is impossible to assign a value directly to a variable.

Therefore the following notation is disallowed:

var number = 20.3.ToRx();
number = 10; // Not possible

Instead, RxVar provides the following syntax:

number.Value = 10;
number.Set (10);

RxProperty for WPF

Rx.Net.Plus also provides means to leverage RxVar to WPF.

The name of class to use for properties is: RxProperty and it is directly derived from RxVar.

For instance:

public class ViewModel :  IPropertyChangedProxy
{
	public RxProperty IsFunctionEnabled { get; } = false.ToRxProperty();
	public RxProperty Counter { get; }
	public RxProperty Message { get; }
}

and in XAML:

< CheckBox  IsChecked="{Binding IsFunctionEnabled}"/>
< TextBox   Text="{Binding Counter}"/>
< TextBox   Text="{Binding Message}"/>

Note that Rx.Net.Plus supports TwoWay binding mode (where target can update source as for CheckBox).

For data binding of RxProperty to work as expected, the view model shall implement a dedicated interface named IPropertyChangedProxy.

Assuming the View Model implements INotifyPropertyChanged, it shall also implements IPropertyChangedProxyas following:

void IPropertyChangedProxy.NotifyPropertyChanged(PropertyChangedEventArgs eventArgs)
{
	OnPropertyChanged (eventArgs);
}

Binding shall occur after the view is created and bound to view model.

In Caliburn framework, this occurs within the OnViewAttached method as following:

class ViewModel : Screen, IPropertyChangedProxy
{
    protected override void OnViewAttached(object view, object context)
    {
        base.OnViewAttached(view, context);
        this.BindRxPropertiesToView (view);			// Bind RxProperties to view
    }
}

In classic MVVM or other frameworks (like Prism, MVVM Light...), call to BindRxPropertiesToView can be handled in Loaded event handler.

Extension Methods

| Method | Description | Usage |
| ---------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RedirectTo | an alias of Subscribe | rxVar.RedirectTo(rxVar2) |
| Notify | an alias of Subscribe | rxVar.Notify(rxVar2) |
| When | equivalent of Where (value => value.Equals(v)) | rxVar.When(true).Notify (rxVar2) |
| ToRxVar | creates an instance of RxVar of the type of var | var rxVar = 10.ToRxVar() |
| ToRxVarAndSubscribe | creates an instance of RxVar of the same type of observable source and subscribe the new instance to source | IObservable&lt;int&gt; obs; var rxvar = obs.Where(v =&gt; v &gt; 10).ToRxVarAndSubscribe(); |
| ToRxProperty | creates an instance of RxProperty of the type of var | RxProperty&lt;int&gt; Counter =&gt; 10.ToRxProperty(); |
| ToRxPropertyAndSubscribe** | creates an instance of RxProperty of the same type of observable source and subscribe the new instance to source | RxProperty&lt;bool&gt; IsPresent; |
| BindRxPropertiesToView | bind the rxproperties of the view model vm (which implements IPropertyChangedProxy) to the view. | this.BindRxPropertiesToView(view) |
| True, False, Not | pseudo attributes may be used as replacement for (var == true, var == false) | isConnected.True, isConnected.False, isConnected.Not |
| DisposableBaseClass | an abstract class to derive from to support Dispose Pattern with some extras | |

Contact us !

For any comment, suggestion...please leave a comment to reactivesw@outlook.com

Rx.Net.Plus

Bring reactiveness to variables and properties

ReactiveX gains popularity and is widely used in several platforms and languages.

If you have used Rx.Net (System.Reactive), you probably already dreamed to transform regular variable (like int, bool ) to become reactive.

What do we mean?

We would like to:

  1. React whenever variables change
  2. Connect variables to observable sources
  3. Not receive notification if same data is set again and again
  4. Continue to use these variables as before. Compare them to values, convert them...

Rx.Net.Plus introduces two new types for this purpose (+ several extension methods):

| Type | Purpose |
| :--------- | ------------------------------------------------------------ |
| RxVar | replace regular variables (like int, bool...) |
| RxProperty | to replace view-model properties (NotifyPropertyChanged pattern) |

RvVar

Let start with an example:

 var isConnected = false.ToRx(); // Create a reactive variable`

Let say, connectionStatus is of type Observable

Observable connectionStatus;

we can do:

connectionStatus.Subscribe (isConnected);

or using our method:

isConnected.ListenTo (connectionStatus);

Now, RxVar's magic is to allow the following syntax:

if (isConnected != true)
{
	// Do something.
}

As well:

var anotherRxBool = true.ToRx();

if (isConnected == connectionStatus)
{
	// Do some action
}

With numeric variables (int, double...)

var intRx = 10.ToRx();               // Create a rxvar

if (intRx > 5)
{
	// React !!
}

RxVar could be used as part of state machine, and instead of polling statuses, flags, counters...

Just react upon change in this manner:

isConnected.When(true).Notify (v => reactToEvent());

RxVar implements the following interfaces:

ISubject, IConvertible, IDisposable, IComparable, IEquatable

Distinct mode

By default, RxVar propagates its data (on update) only when a new distinct value is set.

That means that if some observer is subscribed to RxVar and the same value is assigned to RxVar, it will not be published.

In order to allow every update to be propagated, RxVar provide the following method:

SetDistinctMode

Limitations

As C# does not support overloading of the assignment operator, it is impossible to assign a value directly to a variable.

Therefore the following notation is disallowed:

var number = 20.3.ToRx();
number = 10; // Not possible

Instead, RxVar provides the following syntax:

number.Value = 10;
number.Set (10);

RxProperty for WPF

Rx.Net.Plus also provides means to leverage RxVar to WPF.

The name of class to use for properties is: RxProperty and it is directly derived from RxVar.

For instance:

public class ViewModel :  IPropertyChangedProxy
{
	public RxProperty IsFunctionEnabled { get; } = false.ToRxProperty();
	public RxProperty Counter { get; }
	public RxProperty Message { get; }
}

and in XAML:

< CheckBox  IsChecked="{Binding IsFunctionEnabled}"/>
< TextBox   Text="{Binding Counter}"/>
< TextBox   Text="{Binding Message}"/>

Note that Rx.Net.Plus supports TwoWay binding mode (where target can update source as for CheckBox).

For data binding of RxProperty to work as expected, the view model shall implement a dedicated interface named IPropertyChangedProxy.

Assuming the View Model implements INotifyPropertyChanged, it shall also implements IPropertyChangedProxyas following:

void IPropertyChangedProxy.NotifyPropertyChanged(PropertyChangedEventArgs eventArgs)
{
	OnPropertyChanged (eventArgs);
}

Binding shall occur after the view is created and bound to view model.

In Caliburn framework, this occurs within the OnViewAttached method as following:

class ViewModel : Screen, IPropertyChangedProxy
{
    protected override void OnViewAttached(object view, object context)
    {
        base.OnViewAttached(view, context);
        this.BindRxPropertiesToView (view);			// Bind RxProperties to view
    }
}

In classic MVVM or other frameworks (like Prism, MVVM Light...), call to BindRxPropertiesToView can be handled in Loaded event handler.

Extension Methods

| Method | Description | Usage |
| ---------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| RedirectTo | an alias of Subscribe | rxVar.RedirectTo(rxVar2) |
| Notify | an alias of Subscribe | rxVar.Notify(rxVar2) |
| When | equivalent of Where (value => value.Equals(v)) | rxVar.When(true).Notify (rxVar2) |
| ToRxVar | creates an instance of RxVar of the type of var | var rxVar = 10.ToRxVar() |
| ToRxVarAndSubscribe | creates an instance of RxVar of the same type of observable source and subscribe the new instance to source | IObservable&lt;int&gt; obs; var rxvar = obs.Where(v =&gt; v &gt; 10).ToRxVarAndSubscribe(); |
| ToRxProperty | creates an instance of RxProperty of the type of var | RxProperty&lt;int&gt; Counter =&gt; 10.ToRxProperty(); |
| ToRxPropertyAndSubscribe** | creates an instance of RxProperty of the same type of observable source and subscribe the new instance to source | RxProperty&lt;bool&gt; IsPresent; |
| BindRxPropertiesToView | bind the rxproperties of the view model vm (which implements IPropertyChangedProxy) to the view. | this.BindRxPropertiesToView(view) |
| True, False, Not | pseudo attributes may be used as replacement for (var == true, var == false) | isConnected.True, isConnected.False, isConnected.Not |
| DisposableBaseClass | an abstract class to derive from to support Dispose Pattern with some extras | |

Contact us !

For any comment, suggestion...please leave a comment to reactivesw@outlook.com

Release Notes

First release

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.13 209 6/16/2020
1.1.12 1,324 10/10/2019
1.1.11 118 10/3/2019
1.1.10 806 8/1/2019
1.1.9 1,578 9/16/2018
1.1.8 1,337 9/4/2018
1.1.7 232 9/3/2018
1.1.6 236 9/2/2018
1.1.5 222 8/29/2018
1.1.4 234 8/27/2018
1.0.12 276 8/23/2018
1.0.11 208 8/23/2018
1.0.10 229 8/22/2018
1.0.9 276 8/2/2018
1.0.8 274 8/2/2018
1.0.7 246 7/30/2018
1.0.6 273 7/30/2018
1.0.5 264 7/30/2018
Show less