Skip to content

Include properties in generated ObservableObject class #1061

Closed as duplicate of#1069
@weitzhandler

Description

@weitzhandler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions