[Source Generators] Add ability to define access modifier for `ObservablePropertyAttribute`
jhm-ciberman opened this issue · comments
Overview
For source generators, a lot of times it is needed to expose only the getter of a property and make the setter protected (or even private).
The use case is when a property should only be changed from inside a command and not from the outside.
Currently, adding an [ObservableProperty]
attribute, makes the property public get
and public set
.
API breakdown
I would add an optional parameter to the ObservablePropertyAttribute
constructor:
public ObservablePropertyAttribute();
public ObservablePropertyAttribute(PropertyAccess setter);
public ObservablePropertyAttribute(PropertyAccess setter, PropertyAccess getter);
The enum:
// Same order as: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
public enum PropertyAccess
{
Public = 0,
Private,
Protected,
Internal,
ProtectedInternal,
PrivateProtected,
}
Usage example
[ObservableProperty(setter: PropertyAccess.Protected)]
private int _counter = 0;
will generate something like this:
private int _counter = 0;
public Counter
{
get => _counter;
protected setter => SetProperty(ret _counter, value);
}
Breaking change?
No
Alternatives
The alternative is not use source generators at all. Or expose the setter, knowing that is a design flaw in the ViewModel API.
Additional context
No response
Help us help you
No, just wanted to propose this