ReactiveUI ObservableAsPropertyHelper vs. normal backing variable
Asked Answered
G

1

6

I'm struggling with ReactiveUI's learning curve so this question might be naive. Please help me understand the difference between:

ObservableAsPropertyHelper<string> _input
public string Input {get {return _input.Value;}}

and a normal backing variable with RaiseAndSetIfChanged:

private string _input;
public string Input {
    get {return _input;}
    set {RaiseAndSetIfChanged(ref _input, value);}
}

Are they 2 ways to skin the same cat or are there different use cases/intent for the two options?

Geraldina answered 9/12, 2018 at 23:0 Comment(0)
S
4

ObserableAsProperyHelper (OAPH) helps you wrap a Obserable into a property. So it will provide INotifyPropertyChanged (INPC) notifications for when a new value is placed into your observable.

The second method provides a standard property with INPC notifications.

Subtemperate answered 9/12, 2018 at 23:26 Comment(3)
When should I use one over the other?Geraldina
You often use OAPH when you are generating a new property from values of other properties by using the WhenAnyValue() methods. Or if you have a Observable you pass in from elsewhere. Eg file system statuses etc. Ohservable is closely related to events.Subtemperate
If you look at the example in the docs reactiveui.net/docs/handbook/oaph they are essentially having a ohservable that fires whenever the Name property changes, seperate it based on spaces and extract the first name, they then use OAPH to keep track of the latest values and it auto triggers the INotifyPropertyChange events. OAPH are always read only properties since they are mutated by the observable event being fired.Subtemperate

© 2022 - 2024 — McMap. All rights reserved.