CommunityToolkit / CommunityToolkit/dotnet

ObservableObject annotation with record not generating OnPropertyChanged and OnPropertyChanging invokers

Open
#785 2 comments 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

When using the annotations `[ObservableObject]` and `[ObservableProperty]` in a `class`, it correctly generates (at least) two files. One containing the property which when set calls the functions OnPropertyChanging/OnPropertyChanged and another one containing those functions and the handlers for those events.

If we change the `class` to a `record`, the first file is correctly generated but not the second, causing the OnPropertyChanging/OnPropertyChanged calls not to compile.

Here's an example project: [ObservablePropertyInRecord.zip](https://github.com/CommunityToolkit/dotnet/files/13213997/ObservablePropertyInRecord.zip)

Changing the RecordViewModel from a `record` to a `class` behaves as expected.

### Regression

_No response_

### Steps to reproduce

```text
Example steps to reproduce:

1. Create a `public partial record` with the `[ObservableObject]` annotation
2. Create a variable with the `[ObservableProperty]` annotation
3. Code should not compile and give the errors:

CS0103 The name 'OnPropertyChanging' does not exist in the current context
CS0103 The name 'OnPropertyChanged' does not exist in the current context
```

### Expected behavior

The code generator successfully creates the handlers for a `record` as it does for a `class`.

### Screenshots

ViewModel as a `class`:

![Screenshot 2023-10-31 093049](https://github.com/CommunityToolkit/dotnet/assets/6838045/a325405e-51b5-45db-95f5-949094acd136)

Generated code:

![Screenshot 2023-10-31 093134](https://github.com/CommunityToolkit/dotnet/assets/6838045/c8ee3093-31a6-46a4-a7e9-384f3a78ac1c)
![Screenshot 2023-10-31 093146](https://github.com/CommunityToolkit/dotnet/assets/6838045/4a7d03e1-d085-43a7-83f5-4d9cea3e5251)

ViewModel as a `record`:

![Screenshot 2023-10-31 093205](https://github.com/CommunityToolkit/dotnet/assets/6838045/7059fbbd-a950-4289-812c-c2c271fc146b)

Generated code (the second file is missing):

![Screenshot 2023-10-31 093214](https://github.com/CommunityToolkit/dotnet/assets/6838045/0bbe705e-ce22-4322-914b-0e0d0f4b5275)
![Screenshot 2023-10-31 093228](https://github.com/CommunityToolkit/dotnet/assets/6838045/d1c9dee4-e69d-46f9-b92f-8c9dc98c5cf1)

### IDE and version

VS 2022

### IDE version

17.7.6

### Nuget packages

- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

### Nuget package version(s)

8.2.2

### Additional context

_No response_

### Help us help you

No, just wanted to report this

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.