C
C#11mo ago
CrosRoad95

Blazor "auto-bindable" property source generator

anyone know lib that can auto generate required event and property? For example:
[BindableParameter]
public bool Visible { get; set; }
[BindableParameter]
public bool Visible { get; set; }
Auto generate into:
private bool _visible;

[Parameter]
public bool Visible
{
get => _visible;
set
{
if (_visible == value) return;

_visible = value;
VisibleChanged.InvokeAsync(value);
}
}
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
private bool _visible;

[Parameter]
public bool Visible
{
get => _visible;
set
{
if (_visible == value) return;

_visible = value;
VisibleChanged.InvokeAsync(value);
}
}
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
???
8 Replies
Thinker
Thinker11mo ago
You would have to put the attribute on a field instead.
[BindableParameter]
private bool _visible;
[BindableParameter]
private bool _visible;
You can't replace code so you wouldn't be able to transform public bool Visible { get; set; } into something else wait nvm
CrosRoad95
CrosRoad95OP11mo ago
hmm it's fine, but do you know lib that can do it for me? As you can see, it is a lot of boilerplate
Thinker
Thinker11mo ago
You could try CommunityToolkit.Mvvm
Pobiega
Pobiega11mo ago
CommunityToolkit.Mvvm does exactly this, I believe the attribute is called ObservableProperty https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/overview
public string? Name
{
get => name;
set
{
if (!EqualityComparer<string?>.Default.Equals(name, value))
{
string? oldValue = name;
OnNameChanging(value);
OnNameChanging(oldValue, value);
OnPropertyChanging();
name = value;
OnNameChanged(value);
OnNameChanged(oldValue, value);
OnPropertyChanged();
}
}
}

partial void OnNameChanging(string? value);
partial void OnNameChanged(string? value);

partial void OnNameChanging(string? oldValue, string? newValue);
partial void OnNameChanged(string? oldValue, string? newValue);
public string? Name
{
get => name;
set
{
if (!EqualityComparer<string?>.Default.Equals(name, value))
{
string? oldValue = name;
OnNameChanging(value);
OnNameChanging(oldValue, value);
OnPropertyChanging();
name = value;
OnNameChanged(value);
OnNameChanged(oldValue, value);
OnPropertyChanged();
}
}
}

partial void OnNameChanging(string? value);
partial void OnNameChanged(string? value);

partial void OnNameChanging(string? oldValue, string? newValue);
partial void OnNameChanged(string? oldValue, string? newValue);
this is what it generates for
[ObservableProperty]
private string? name;
[ObservableProperty]
private string? name;
CrosRoad95
CrosRoad95OP11mo ago
let's code it myself
No description
CrosRoad95
CrosRoad95OP11mo ago
Should not be that hard
CrosRoad95
CrosRoad95OP11mo ago
No description
Pobiega
Pobiega11mo ago
Look up how the mvvm lib is written, it's open source.
Want results from more Discord servers?
Add your server