reactiveui / ReactiveUI

An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms that is inspired by functional reactive programming. ReactiveUI allows you to abstract mutable state away from your user interfaces, express the idea around a feature in one readable place and improve the testability of your application.
https://www.reactiveui.net
MIT License
8.11k stars 1.12k forks source link

Source generators #3864

Closed ThatRendle closed 4 months ago

ThatRendle commented 4 months ago

Is your feature request related to a problem? Please describe.

PropertyChanged and Commands required a lot of verbose, repetitive boilerplate code.

Describe the solution you'd like

Source generators could generate properties and commands from fields and methods, applying best practices along the way.

Describe suggestions on how to achieve the feature

I'm using some very basic source generators in an Avalonia UI project at the moment, for example the Command generator can only handle parameterless methods right now, but it's a start.

I copied the implementations to a gist: https://gist.github.com/markrendle/93ccfb62f2e2f87137148b9aaa57f02f

public partial class LoginViewModel : ViewModelBase
{
    [ReactiveProperty] private string? _userName; // Generates UserName property
    [ReactiveProperty] private string? _password; // Generates Password property
    [ReactiveProperty] private bool _loggingIn; // Generates LoggingIn property

    [ReactiveCommand] // Generates LoginCommand command using CanLogin as canExecute
    private void Login()
    {
        LoggingIn = true;
        _userManager.Login(UserName, Password);
    }

    private bool CanLogin() => !LoggingIn && UserName is { Length: > 0 } && Password is { Length: > 0 };
}

Additional context

Obviously this is inspired by https://github.com/CommunityToolkit/dotnet/tree/main/src/CommunityToolkit.Mvvm.SourceGenerators

ChrisPulman commented 4 months ago

@markrendle please see our Source Generators, if you need anything further please raise your request in the source generators repository. https://www.nuget.org/packages/ReactiveUI.SourceGenerators

ReactiveUI.SourceGenerators 1.0.3
A MVVM framework that integrates with the Reactive Extensions for .NET to create elegant, testable User Interfaces that run on any mobile or desktop platform. This is the Source Generators package for ReactiveUI
ChrisPulman commented 4 months ago

You can find out how to use the Source Generators here: https://www.reactiveui.net/docs/handbook/view-models/boilerplate-code.html

The manual way to create properties in ReactiveUI | An advanced, composable, reactive model-view-viewmodel framework
ChrisPulman commented 4 months ago

Closing this as the requested feature already exists

github-actions[bot] commented 3 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.