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

Visibility modifier for [ObservableProperty] Source Generator #668

Closed
ewerspej opened this issue Apr 15, 2023 · 1 comment
Closed

Visibility modifier for [ObservableProperty] Source Generator #668

ewerspej opened this issue Apr 15, 2023 · 1 comment
Labels
duplicate 👥 Indicates that an identical issue or PR already exists feature request 📬 A request for new changes to improve functionality mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit

Comments

@ewerspej
Copy link

Overview

There is currently no way to modify the visibility of the property setter of a generated property. There are sometimes situations where you would mark a setter as internal, private or protected (or even internal protected), so that a property can only be set by certain actors, while the getter should always be public, so that it can be used in binding expressions.

For example, it would be useful if the [ObservableProperty] Source Generator would be able to generate the equivalent of the following:

private string identifier;
public string Identifier
{
    get => identifier;
    private set
    {
        if(identifier.Equals(value)) return;
        identifier = value;
        OnPropertyChanged();
    }
}

The Identifier property could then still be used in a binding expression:

<Label Text="{Binding Identifier}" />

The value can only be set from inside the class, e.g. in a method or command that calls some API:

[RelayCommand]
private async Task UpdateAsync()
{
    Identifier = await RestClient.SomeUpdateCallAsync();
}

This is useful, for example, if the value of the Identifier comes from an API call and shouldn't be publicly settable, or when it should not participate in Two-Way bindings. Therefore there should be a way to modify the visibility.

API breakdown

My proposal is to add an optional visibility modifier to the [ObservableProperty] Source Generator called SetterVisibility which takes a single argument, e.g. an enum value or some other limited form of identifier:

//generates property MyBoolA with private setter
[ObservableProperty(SetterVisibility = Visibility.Private)]
private bool myBoolA;

//generates property MyBoolB with internal setter
[ObservableProperty(SetterVisibility = Visibility.Internal)]
private bool myBoolB;

//generates property MyBoolC with protected setter
[ObservableProperty(SetterVisibility = Visibility.Protected)]
private bool myBoolC;

The resulting properties should then be equivalent to the following:

private bool myBoolA;
public bool MyBoolA
{
    get => myBoolA;
    private set
    {
        if(myBoolA.Equals(value)) return;
        myBoolA = value;
        OnPropertyChanged();
    }
}

private bool myBoolB;
public bool MyBoolB
{
    get => myBoolB;
    internal set
    {
        if(myBoolB.Equals(value)) return;
        myBoolB = value;
        OnPropertyChanged();
    }
}

private bool myBoolC;
public bool MyBoolC
{
    get => myBoolC;
    protected set
    {
        if(myBoolC.Equals(value)) return;
        myBoolC = value;
        OnPropertyChanged();
    }
}

The default will either be Visibility.Public or null which will avoid breaking existing behavior.

Usage example

MyViewModel

public class MyViewModel : ObservableObject
{
    [ObservableProperty(SetterVisibility = Visibility.Private)]
    private string identifier;

    [RelayCommand]
    private async Task UpdateIdentifierAsync()
    {
        Identifier = await SomeRestApi.GetValueAsync();
    }
}

MyView.xaml

<StackLayout>
    <Label Text="{Binding Identifier}" />
    <Button Text="Update Identifier" Command="{Binding UpdateIdentifierCommand}" />
</StackLayout>

Breaking change?

No

Alternatives

n/a

Additional context

No response

Help us help you

Yes, but only if others can assist

@ewerspej ewerspej added the feature request 📬 A request for new changes to improve functionality label Apr 15, 2023
@Sergio0694
Copy link
Member

Duplicate of #291.
Superseded by #555.

@Sergio0694 Sergio0694 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 15, 2023
@Sergio0694 Sergio0694 added duplicate 👥 Indicates that an identical issue or PR already exists mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit labels Apr 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate 👥 Indicates that an identical issue or PR already exists feature request 📬 A request for new changes to improve functionality mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit
Projects
None yet
Development

No branches or pull requests

2 participants