CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
2.99k stars 294 forks source link

[ObservablePropertyAttribute] add modifiers for generated code #814

Closed Palatis closed 9 months ago

Palatis commented 9 months ago

Overview

currently all property code generated by the analyzer are with no access modifiers, for example:

[ObservableProperty]
private bool value;

generates

public bool Value {
    get => value;
    set { ... }
}

but sometimes we might want an observable property with a little more privacy... for example, some property that can only be modified by the class itself:

public bool Value {
    get => value;
    private set { ... } // or protected, internal... etc.
}

API breakdown

[Flags]
public enum AccessModifier
{
    None = 0x00,
    Public = 0x01,
    Protected = 0x02,
    Internal = 0x04,
    Private = 0x08,
    ProtectedInternal = Protected | Internal,
    PrivateProtected = Private | Protected,
}

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public sealed class ObservablePropertyAttribute : Attribute
{
    public AccessModifier PropertyModifier { get; }
    public AccessModifier GetModifer { get; }
    public AccessModifier SetModifier { get; }

    public ObservablePropertyAttribute() :
        this(AccessModifier.None, AccessModifier.None, AccessModifier.None)
    { }

    public ObservablePropertyAttribute(AccessModifier setModifier) :
        this(AccessModifier.None, setModifier)
    { }

    public ObservablePropertyAttribute(AccessModifier getModifier, AccessModifier setModifier) :
        this(AccessModifier.None, getModifier, setModifier)
    { }

    public ObservablePropertyAttribute(
        AccessModifier propertyModifier,
        AccessModifier getModifier,
        AccessModifier setModifier
    )
    {
        PropertyModifier = propertyModifier;
        GetModifier = getModifier;
        SetModifier = setModifier;
    }
}

Usage example

[ObservableProperty]
private bool value1;
[ObservableProperty(AccessModifier.Private)]
private bool value2;
[ObservableProperty(AccessModifier.Protected, AccessModifier.None, AccessModifier.Internal)]
private bool value3;

now generates

public bool Value1 { get => value1; set => { ... } }
public bool Value2 { get => value2; private set => { ... } }
protected bool Value3 { get => value3; internal set => { ... } }

Breaking change?

No

Alternatives

none, I just manually code the property instead of using ObservablePropertyAttribute.

Additional context

none

Help us help you

Yes, but only if others can assist

Palatis commented 9 months ago

another reason for this is that we can use NotifyPropertyChangedFor and such on the generated property.

Sergio0694 commented 9 months ago

Duplicate of #291. Superseded by #555.

Palatis commented 9 months ago

okay, then.