CommunityToolkit / CommunityToolkit/dotnet
Better support for notifying cascading calculated properties
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 400
- PR merge metrics
- No merged PRs in 30d
Description
### Overview
I have a particularly complex scoring UI that I'm trying to model which has a number of inputs (in the 15-20 input range per side, and there are 2 sides in each match). These inputs then cascade into a dozen outputs from the match, all of which I would like to display in the UI. Further, these outputs often influence each other. As a concrete example:
* This UI reflects game pieces scored in one of 12 positions.
* Scoring in one of these affects the total number of points scored by that side, which affects who won the match.
* Scoring in one can also affect secondary outputs; during qualification rounds, teams can get extra points for doing what I'll call "playing the game extremely well". The number of game pieces scored affects this value.
These calculations are easier to display to the user broken up, rather than just "here's the final value". For example, we display both the number of total game pieces scored across all components, as well as the final score. However, because each of these inputs can have multiple cascading impacts (scoring in a single location impacts total number of items scored, which impacts total number of points), I have to do a _lot_ of `NotifyPropertyChangedFor` duplication across all of these inputs. I would prefer to annotate the calculated properties with what they themselves will cause to change; then all I have to do is annotate the inputs with the outputs they _directly_ impact, and let secondary impacts be sorted out through a graph walk by the source generator.
### API breakdown
No new APIs are necessary. Just add `AttributeTargets.Property` to `NotifyPropertyChangedFor`, and update the source generator to understand that it should explore all `NotifyPropertyChangedFor` properties for cascaded properties.
### Usage example
A very simplified example. Note that this sample is missing several inputs to the calculated properties I'm showing.
```cs
public class AllianceScore : ObservableObject
{
[ObservableProperty, NotifyPropertyChangedFor(nameof(TotalSpeakerNotes), nameof(TotalSpeakerPoints))]
private int speakerNotesAuto;
[ObservableProperty, NotifyPropertyChangedFor(nameof(TotalSpeakerNotes), nameof(TotalSpeakerPoints))]
private int speakerNotesTeleop;
[ObservableProperty, NotifyPropertyChangedFor(nameof(TotalSpeakerNotes), nameof(TotalSpeakerPoints))]
private int speakerNotesAmplifiedTeleop;
// This is the new bit. It would cause the generated properties to also notify for MelodyBonusAchieved, so that piece of UI is updated as well as this one.
[NotifyPropertyChangedFor(nameof(MelodyBonusAchieved))]
public int TotalSpeakerNotes => SpeakerNotesAuto + SpeakerNotesTeleop + SpeakerNotesAmplifiedTeleop;
public bool MelodyBonusAchieved => TotalSpeakerNotes >= MELODY_THRESHOLD;
// Another example of a new bit.
[NotifyPropertyChangedFor(nameof(TotalPoints))]
public int TotalSpeakerPoints => SpeakerNotesAuto * SPEAKER_AUTO_POINTS + SpeakerNotesTeleop * SPEAKER_TELEOP_POINTS + SpeakerNotesAmplifiedTeleop * SPEAKER_TELEOP_AMPLIFIED_POINTS;
public int TotalPoints => TotalSpeakerPoints + /* other point inputs here */
}
```
### Breaking change?
No
### Alternatives
Currently, I just have to duplicate these `NotifyPropertyChangedFor` across all the inputs. It's a lot of duplication, and is very fragile. I don't want to maintain it 🙂.
### Additional context
_No response_
### Help us help you
No, just wanted to propose this
Contributor guide
Assessment
This issue has not been assessed yet.