CommunityToolkit / CommunityToolkit/dotnet
Generate source for RelayCommandAttribute on record as well
- 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
Assessment
This issue has not been assessed yet.