Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] ObservableAsPropertyAttribute weaver handling initial value #3304

Open
JeremyBP opened this issue Jul 5, 2022 · 2 comments
Open
Assignees

Comments

@JeremyBP
Copy link

JeremyBP commented Jul 5, 2022

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)

@ChrisPulman
Copy link
Member

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

@ChrisPulman ChrisPulman self-assigned this Aug 3, 2024
@ChrisPulman
Copy link
Member

ChrisPulman commented Aug 11, 2024

OAPH initial Value
This should be included in the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants