CommunityToolkit/dotnet

Option to enable `required` on a property generated by `ObservableProperty`

Closed this issue · 2 comments

Overview

Currently there is no option to add the required keyword to the property generated by the ObservableProperty. This is unwanted, because required is there for a reason: in many cases it's not correct for a property to ever be null[able] (The whole nullable/init/require stuff is really usefull in creating a safe application, so it would be weird not to support it).

So I suggest an option to enable required on the generated property is added.

API breakdown

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

Usage example

public sealed partial class MyType : ObservableObject
{
    [ObservableProperty(IsRequired = true]
    private string name;
}

Breaking change?

No

Alternatives

Alternatively? Maybe a separate attribute RequiredObservablePropertyAttribute?

Additional context

No response

Help us help you

Yes, but only if others can assist

Closing this, superseded by #555.

Ah, and now see it's a dupe of #679
thx