I currently have a ViewModel where I implement (by hand) the "Lazy" initialization model (code below).

The idea is that I have a data pipeline and I want to display some of its intermediary results in a View. And, very important, I want the properties to be lazy, so that they are "pulled" from the ViewModel only when the View calls their getter.

The current pattern works fine for one "run" of the calculations, but since each calculation step depends on some numeric parameters, and I want to reflect the change in parameter values downstream the pipeline, thus updating the values and the display accordingly, I would like to once and for all grasp the way of doing it in ReactiveUI.

I suppose some pattern involving ReactiveObject, WhenAny, WhenAnyValue and ObservableAsPropertyHelper must common, but this is like the fourth way I try to do this kind of thing, and I always have a hard time understanding the concepts.

public class OldViewModel : ViewModelBase
    public IEnumerable<double> EnvelopeEmg 
        => _envelopeEmg ??
           (_envelopeEmg = Algoritmos.CriarEnvelopeEmg(_model.Emg));
    IEnumerable<double> _envelopeEmg;

    public IEnumerable<DataPoint> EnvelopeSeries
        => _envelopeSeries ?? 
           (_envelopeSeries = Algoritmos.CriarEnvelopeSeries(EnvelopeEmg));
    IEnumerable<DataPoint> _envelopeSeries;

    public ExameErgoEmgViewModel(ExameErgoEmg model)
        _model = model;

public class NewViewModel : ReactiveObject
    public IEnumerable<double> EnvelopeEmg { get; }
    IEnumerable<double> _envelopeEmg;   /// OAPH here?

    public IEnumerable<DataPoint> EnvelopeSeries { get; }
    IEnumerable<DataPoint> _envelopeSeries;   /// OAPH here?

    public ExameErgoEmgViewModel(ExameErgoEmg model)
        _model = model;

        // ????
        // What should be initialized here? And how?
        // How to lazily get the first value of EnvelopeEmg?
        // How to reflect changes in EnvelopeEmg to EnvelopeSeries property?
  • 1
    You can make envelopeEmg an oaph and when you set it up in the constructor with ToProperty, set the deferSubscrption parameter to true. That will achieve the lazy first value functionality. – Colt Bauman Apr 16 at 22:54

Your Answer


Browse other questions tagged or ask your own question.