rrmanzano / maui-bindableproperty-generator

Source generator that automatically transforms fields into BindableProperties that can be used in MAUI
MIT License
135 stars 10 forks source link

Think about making the AutoBindable an Attribute for the class, so you do not need a placeholder field #9

Open juwens opened 1 year ago

juwens commented 1 year ago

This :

    [AutoBindable("Header", typeof(string))] // c#10
    [AutoBindable<string>("Header")] // with C#11 generic attributes feature
    public partial class CustomEntry : ContentView
    {
    }

Instead of this:

    public partial class CustomEntry : ContentView
    {
        [AutoBindable]
        private readonly string _placeholder;
    }

Advantage:

Downside:

paulvarache commented 1 year ago

I think it makes a lot of sense. While I like having properties defined inside the class block, having to declare an unused field requires turning off some compiler warnings.

It should be possible to implement this without breaking the existing attribute and either offer both options or provide a transition period for developers to adapt

tuyen-vuduc commented 1 year ago

How do you think if the generator will base on the handler methods?

e.g.

// ControlX.properties.cs
partial class ControlX {
    [AutoBindable<bool>]
     private static void HandlePropertyXChanged(BindableObject obj, object oldValue, object newValue) {}

     private static void HandlePropertyXChanging(BindableObject obj, object oldValue, object newValue) {}

    [AutoBindable<string>()]
     private static void HandlePropertyYChanging(BindableObject obj, object oldValue, object newValue) {}
}

The generator will generate

// ControlX.g.cs
partial class ControlX {
    public static BindableProperty PropertyXProperty = BindableProperty.Create(
        nameof(PropertyX),
        typeof(ControlX),
        typeof(bool),
        true,
        propertyChanged: HandlePropertyXChanged,
        propertyChanging: HandlePropertyXChanging
    );
    public bool PropertyX {
        get => (bool)GetValue(PropertyXProperty);
        get => SetValue(PropertyXProperty, value);
    }

    public static BindableProperty PropertyYProperty = BindableProperty.Create(
        nameof(PropertyY),
        typeof(ControlX),
        typeof(string),
        default,
        propertyChanging: HandlePropertyYChanging
    );
    public string PropertyY {
        get => (string)GetValue(PropertyYProperty);
        get => SetValue(PropertyYProperty, value);
    }
}
tuyen-vuduc commented 1 year ago

And we should remove MAUI dependency because with .NET6/C# 10, we can use global usings for namespaces in generated code