reactiveui/ReactiveUI

[Feature] ObservableAsPropertyAttribute weaver handling initial value

JeremyBP opened this issue · 2 comments

Is your feature request related to a problem? Please describe.
Sometime we need to define a property in a parent abstract class, but set its value on child ones.
How to deal with it with OAPH fody weaved properties?
I mean I know I could write things manualy like:

Parent class:

private readonly ObservableAsPropertyHelper<string> myStringProperty = ObservableAsPropertyHelper<string>.Default("MyInitialValue");
public string MyStringProperty => myStringProperty.Value;

Child class 1:

this.WhenAnyValue(vm => vm.MyChildOneReactiveProperty, selector: prop => $"Value on child 1: {prop}")
    .ObserveOn(RxApp.MainThreadScheduler)
    .ToPropertyEx(this, vm => vm.MyStringProperty)
    .DisposeWith(DestroyWith);

Child class 2:

this.WhenAnyValue(vm => vm.MyChildTwoReactiveProperty, selector: prop => $"Value on child 2: {prop}")
    .ObserveOn(RxApp.MainThreadScheduler)
    .ToPropertyEx(this, vm => vm.MyStringProperty)
    .DisposeWith(DestroyWith);

That works.
But what if I want to use ObservableAsPropertyAttribute provided by Reactive.Fody?

Well, actually, I could write things like this:
Parent class:

public abstract class MyParentClass
{
    protected MyParentClass()
    {
        Observable.Return("MyInitialValue")
            .ToPropertyEx(this, vm => vm.MyStringProperty)
            .DisposeWith(DestroyWith);
    }

    [ObservableAsProperty] public string MyStringProperty { get; }
}

and my Child classes will override the value with their own WhenAnyValue, that's ok. But it's still kind of a boilerplate to me for just setting an initial value.

Describe the solution you'd like
It could be cool if we could define initial values by writing things like:

[ObservableAsProperty] public string MyStringProperty { get; } = "MyInitialValue";

[ObservableAsProperty] public bool MyBoolProperty { get; } = true;

It seems natural to me.
Don't know if it's possible as I don't know anything about the weaver magic thing actually.
If not, maybe this one should be:

[ObservableAsProperty("MyInitialValue")] public string MyStringProperty { get; }

[ObservableAsProperty(true)] public bool MyBoolProperty { get; }

Less natural, but doing the job.

Describe alternatives you've considered
Described into the problem description section.

Describe suggestions on how to achieve the feature
Take declared default value on the weaver side instead of default(T)

Hi, I will look into trying to achieve this with our ReactiveUI.SourceGenerators

OAPH initial Value
This should be included in the next release