Im using ReactiveProperty library in my code and from time to time I need to attach two ReactiveProperty<T>
together maintaining property data synchronized. For example by combining a ReactivePropertySlim
from a service class to a ReactiveProperty
in a ViewModel class.
Usually I use the next code:
// NewProperty is a ViewModel public property
NewProperty = service.Property.ToReactiveProperty<T>();
var propertyDisposable = NewProperty.Subscribe(value => service.Property.Value = value);
Not lookup so bad for a single property but when the number gets high the code gets to.
For now Im using a simple extension method to limit the code repetition.
public static (IReactiveProperty<T> property, IDisposable cleanup) AttachReactiveProperty<T>(this IReactiveProperty<T> baseProperty)
{
var newProperty = baseProperty.ToReactiveProperty<T>();
var cleanup = newProperty.Subscribe(value => baseProperty.Value = value);
return (newProperty, cleanup);
}
I end with a property variable and a IDisposable variable to manage unsubscrition.
var (pausedProperty, pausedDisposable) = remoteConversion.Paused.AttachReactiveProperty();
NewProperty = pausedProperty;
For now the extension is doing his work (less and clear code I think). But is there a better way to approach this problem.