Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include properties in generated ObservableObject class #1061

Closed
weitzhandler opened this issue Mar 13, 2025 · 1 comment
Closed

Include properties in generated ObservableObject class #1061

weitzhandler opened this issue Mar 13, 2025 · 1 comment
Labels
feature request 📬 A request for new changes to improve functionality

Comments

@weitzhandler
Copy link

weitzhandler commented Mar 13, 2025

Overview

The ObservableObjectAttribute is very powerful in cutting a lot of boiler plate code and 'injecting' INPC stuff into a class.

I was wondering if we could take it a step further and wrap all of the source's virtual properties as well.

This is super useful when the entities are pre-existing or are coming from a 3rd party library. Rewriting all models with their properties is super tedious.

API breakdown

public class PersonModel
{
    public virtual string Name { get; set; }    
    public virtual int Age { get; set; }
    public virtual string Address { get; set; }
}

[ObservableObject(RegenerateProperties = true, ExcludeProperties = "Name,Address")]
public partial class PersonViewModel : Model
{
}

Generated:

public partial class PersonViewModel : Model, INotifyPropertyChanged, INotifyPropertyChanging
{

    // Everything that's already generated today
   
    public override int Age
    {
        get => _Age;
        set
        {
                if (!EqualityComparer<int>.Default.Equals(_Age, value))
                {
                    OnAgeChanging(value);
                    OnAgeChanging(default, value);
                    OnPropertyChanging(...);
                    _Age = value;
                    OnAgeChanged(value);
                    OnAgeChanged(default, value);
                    OnPropertyChanged(...);
                }
            }
        }
    }

    partial void OnAgeChanging(int value);
    ...        
}

Usage example

public void Main()
{
    var model = new PersonViewModel { Name = "John", Age = 30 };
    model.PropertyChanged = OnPersonChanged;
    model.Age = 42; // OnPersonChanged fired
}

Breaking change?

No

Alternatives

Castle.Core

Additional context

No response

Help us help you

Yes, I'd like to be assigned to work on this item

@weitzhandler weitzhandler added the feature request 📬 A request for new changes to improve functionality label Mar 13, 2025
@weitzhandler weitzhandler changed the title Complete ObservableObject proxy class? Include properties in generated ObservableObject class Mar 13, 2025
@weitzhandler
Copy link
Author

Oops
I didn't remember I've already posted this question and posted a new one #1069.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality
Projects
None yet
Development

No branches or pull requests

1 participant