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
3.07k stars 299 forks source link

Add support for "required" and "virtual" keywords to property generator #679

Closed michael-tlsoftware closed 1 year ago

michael-tlsoftware commented 1 year ago

Overview

When working with nullable libraries, being able to add the required keyword to properties is helpful. I have reviewed the source generator and dont believe this is currently supported when using ObservablePropertyAttribute . I propose adding properties to the this class to signal to the generator to add this modifier where required. This would default to false to not impact existing usage.

As the code would be similar, I propose to add the virtual modifier at the same time for completeness.

API breakdown

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public sealed class ObservablePropertyAttribute : Attribute
{
    public bool Virtual { get; set; } = false;
    public bool Required { get; set; } = false;
}

Usage example

[ObservableProperty(Required = true, Virtual = true)]
bool _myProperty;

Breaking change?

No

Alternatives

Current alternative is to selectively use source generator, and use the underlying methods when creating properties by hand.

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

Sergio0694 commented 1 year ago

Duplicate of #291. Superseded by #555.