CommunityToolkit / CommunityToolkit/dotnet

Generate source for RelayCommandAttribute on record as well

Open
#647 0 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail: mvvm-toolkit :toolbox:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

Records should also be able to generate `RelayCommand` just like classes do.

### API breakdown

No new API

### Usage example

Data layer has a record:
```cs
public record DataRecord(int Stuff, IListListOfStuff);
```

In the MVVM layer we want to provide commands to add or remove from the ListOfStuff:

```cs
public partial record DataRecordVM(DataRecord dr) : DataRecord(dr.Stuff, dr.ListOfStuff)
{
}
```

Inside `DataRecordVM` I'd like to add to the list
```cs
[RelayCommand]
private void Add(int newStuff) => ListOfStuff.Add(newStuff);
```

### Breaking change?

No

### Alternatives

The alternative is to instantiate `RelayCommand` by hand.

The other alternative is to use a class which has the record as its member, but it is too verbose:
```cs
public partial class DataClassVM(DataRecord dr)
{
[RelayCommand]
private void Add(int newStuff) => dr.ListOfStuff.Add(newStuff);

// expose if necessary
public IList DataRecord => dr.ListOfStuff;
public int Stuff => dr.Stuff;
}
```

### Additional context

_No response_

### 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.