ReactiveUI icon indicating copy to clipboard operation
ReactiveUI copied to clipboard

Make IObservable::BindTo typesafe.

Open bradphelan opened this issue 8 years ago • 1 comments

Note: for support questions, please ask on StackOverflow: https://stackoverflow.com/questions/tagged/reactiveui . This repository's issues are reserved for feature requests and bug reports.

Bug/Feature

    /// <summary>
    /// BindTo takes an Observable stream and applies it to a target
    /// property. Conceptually it is similar to "Subscribe(x =&gt;
    /// target.property = x)", but allows you to use child properties
    /// without the null checks.
    /// </summary>
    /// <param name="target">The target object whose property will be set.</param>
    /// <param name="property">An expression representing the target
    /// property to set. This can be a child property (i.e. x.Foo.Bar.Baz).</param>
    /// <param name="conversionHint">
    /// An object that can provide a hint for the converter.
    /// The semantics of this object is defined by the converter used.
    /// </param>
    /// <returns>An object that when disposed, disconnects the binding.</returns>
    public static IDisposable BindTo<TValue, TTarget, TTValue>(
        this IObservable<TValue> This,
        TTarget target,
        Expression<Func<TTarget, TTValue>> property,
        object conversionHint = null,
        IBindingTypeConverter vmToViewConverterOverride = null)
    {
        return binderImplementation.BindTo(This, target, property, conversionHint, vmToViewConverterOverride);
    }

Say I have some code

  viewModel.WhenAnyValue( p => p.ToolpathSpecificationViewModel.ToolpathAniminationControlViewModel )
           .WhereNotNull()
           .BindTo( toolpathAnimationControl, p=>p.ViewModel  )
           .DisposeWith( d );

which is all working fine. Then some muppet comes along and adds

  viewModel.WhenAnyValue( p => p.ToolpathSpecificationViewModel.ToolpathAniminationControlViewModel )
           .WhereNotNull()
           .Select( _ => "ha ha evil" )
           .BindTo( toolpathAnimationControl, p=>p.ViewModel  )
           .DisposeWith( d );

It compiles just fine but I end up with a run time crash. I can understand the lure of automatic type converters for XAML as it is very "dynamic" and converters are very verbose to use but this here is code and RX combinators are short and sweet.

If the user needs to convert doubles to strings then they can just use a Select statement to do the conversion or if this proves to be too taxing then they can write a custom combinator that overloads BindTo for their favorite conversion.

bradphelan avatar Apr 21 '17 05:04 bradphelan

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We do this because it helps reduce the workload of our extremely busy maintainers. If you want this issue progressed faster please start talks with a maintainer with how you can help out. There are so many ways to contribute to open-source and the most valued are often not code related. We are always looking for more help and look after those who stick around. Say howdy to one of the maintainers or browse https://reactiveui.net/contribute/ for ideas on where to start your journey.

stale[bot] avatar Aug 25 '17 09:08 stale[bot]