Skip to content

[ObservableProperty] generates partial OnPropertyChanging/Changed methods - #119

Merged
Sergio0694 merged 5 commits into
mainfrom
dev/partial-observable-property-methods
Mar 1, 2022
Merged

Sergio0694 merged 5 commits into
mainfrom
dev/partial-observable-property-methods

Conversation

@Sergio0694

@Sergio0694 Sergio0694 commented Feb 21, 2022 •

Copy link
Copy Markdown
Member

Closes #117

This PR generates OnPropertyChanging and OnPropertyChanged partial methods for [ObservableProperty].

FYI @sonnemaf

Example code

namespace MyProject.ViewModels;

public partial class MyViewModel
{
    [ObservableProperty]
    private string? name;
}

Generated code

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace MyProject.ViewModels
{
    partial class MyViewModel
    {
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCode]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public string? Name
        {
            get => name;
            set
            {
                if (!global::System.Collections.Generic.EqualityComparer<string?>.Default.Equals(name, value))
                {
                    OnNameChanging(value);
                    OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
                    name = value;
                    OnNameChanged(value);
                    OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
                }
            }
        }

        /// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.0.0.0")]
        partial void OnNameChanging(string? value);
        /// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.0.0.0")]
        partial void OnNameChanged(string? value);
    }
}

@Sergio0694 Sergio0694 added feature 💡 A new feature being implemented next preview ✈️ This changes will be available in the upcoming preview priority 🚩 An issue or change that has priority mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit labels Feb 21, 2022
@michael-hawker

Copy link
Copy Markdown
Member

@Sergio0694 in your comment above on the PR:

        /// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
        [global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ICommandGenerator", "8.0.0.0")]
        

Should it be IPropertyChangeGenerator vs. ICommandGenerator? I guess I'm not sure what this generatedcode attribute is for?

@Sergio0694

Copy link
Copy Markdown
Member Author

@michael-hawker Whoops, good catch! 😄
Fixed in 93330b0.

@Sergio0694
Sergio0694 force-pushed the dev/partial-observable-property-methods branch from 93330b0 to 7b08d58 Compare February 22, 2022 18:59

@michael-hawker michael-hawker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a suggestion for another test if you think it'd be good.

@Sergio0694
Sergio0694 force-pushed the dev/partial-observable-property-methods branch from 17c2cf4 to f40eb62 Compare February 28, 2022 23:17
@Sergio0694
Sergio0694 merged commit 3fc61fe into main Mar 1, 2022
@delete-merged-branch
delete-merged-branch Bot deleted the dev/partial-observable-property-methods branch March 1, 2022 12:40
@Sergio0694
Sergio0694 restored the dev/partial-observable-property-methods branch March 8, 2022 23:07
@Sergio0694
Sergio0694 deleted the dev/partial-observable-property-methods branch March 8, 2022 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature 💡 A new feature being implemented mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit next preview ✈️ This changes will be available in the upcoming preview priority 🚩 An issue or change that has priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discussion: Should the MVVM SourceGenerator generate partial methods for ObservableProperty fields

3 participants