CommunityToolkit / CommunityToolkit/dotnet

ObservableProperty Attribute for Class

Open
#754 0 comments 1 reaction 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

To simplify the Observable Property attribute required for all properties inside the class.

Current Usage:
```
public partial class AddContact : ObservableObject
{
[ObservableProperty] private Guid id;
[ObservableProperty] private string firstName;
[ObservableProperty] private string lastName;
[ObservableProperty] private Phone phone;
[ObservableProperty] private DateTime? expiry;
[ObservableProperty] private ObservableCollection extraPhones = new();
[ObservableProperty] private string image;
[ObservableProperty] private Group group;
[ObservableProperty] private string email;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowRingtonePlay))]
[NotifyPropertyChangedFor(nameof(RingtoneFile))]
private string ringtone;
public bool ShowRingtonePlay => !Ringtone.IsNullOrEmpty();
public string RingtoneFile => Path.GetFileName(Ringtone);
[ObservableProperty] private ObservableCollection emails = new();
[ObservableProperty] private Company company;
[ObservableProperty] private Country country;
[ObservableProperty] private Relationship relationship;
[ObservableProperty] private Address address;
[ObservableProperty] private ObservableCollection events = new();
[ObservableProperty] private ObservableCollection socials = new();
[ObservableProperty] private ObservableCollection notes = new();

private readonly CountryService countryService;
}
```
### API breakdown

No Api breakdown

### Usage example

```
[ObservableProperty] or [AllObservableProperty]
public partial class AddContact : ObservableObject
{
private Guid id;
private string firstName;
private string lastName;
private Phone phone;
private DateTime? expiry;
private ObservableCollection extraPhones = new();
private string image;
private Group group;
private string email;
private ObservableCollection emails = new();
private Company company;
private Country country;
private Relationship relationship;
private Address address;
private ObservableCollection events = new();
private ObservableCollection socials = new();
private ObservableCollection notes = new();

[NotifyPropertyChangedFor(nameof(ShowRingtonePlay))]
[NotifyPropertyChangedFor(nameof(RingtoneFile))]
private string ringtone;

public bool ShowRingtonePlay => !Ringtone.IsNullOrEmpty();
public string RingtoneFile => Path.GetFileName(Ringtone);

private readonly CountryService countryService;
}
```

### Breaking change?

No

### Alternatives

No

### Additional context

For much better code.

For automatic code generation, avoid public property and private readonly property.

### Help us help you

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.