dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

There is no way know whether a type implements Command and CommandParameter, request to consider abstracting them into an interface #4396

Open egvijayanand opened 2 years ago

egvijayanand commented 2 years ago

Description

I'm writing an extension method to bind to Command and CommandParameter and make use of it as a Fluent API, but unfortunately, no direct way to check whether the type on which it's invoked implements Command and CommandParameter. As of now, doing a manual check on the types that implement those properties.

If those two (bindable) properties are abstracted into an interface, say ICommanding, it would be easy to add that condition in the generic type check so that this Fluent API can be invoked only on them.

Public API Changes

Existing implementation:

public class Button : View, // other interfaces
{
    // For brevity, only the required code is given below
    public static readonly BindableProperty CommandProperty;
    public static readonly BindableProperty CommandParameterProperty;
    // Property implementation
    public ICommand Command // ...
    public object CommandParameter //...
    // Rest of the implementation
}

Proposed implementation and the same applies for all the types which implement those two properties:

public interface ICommanding
{
    ICommand Command { get; }
    object CommandParameter { get; }
}
public class Button : View, ICommanding, // other interfaces
{
    // For brevity, only the required code is given below
    public static readonly BindableProperty CommandProperty;
    public static readonly BindableProperty CommandParameterProperty;
    // Implementation of ICommanding interface
    public ICommand Command // ...
    public object CommandParameter //...
    // Rest of the implementation
}

Intended Use-Case

The first problem would be that every supported type needs to be included in the list for this feature to work and the bigger issue is when a new type with the command is introduced, the logic needs to be updated to include that type well.

public static TBindable BindCommandWithParameter<TBindable>(
    this TBindable bindable,
    string path = bindingContextPath,
    object? source = null,
    object? parameterValue = null) where TBindable : BindableObject, // Ideally, need have that ICommanding here, 
    // so that this method is available only to the types that have those properties implemented for the interface contract
{
    // Bind the command
    // Explicit value of null to parameterPath as value of CommandParameter is static in nature
    bindable.BindCommand(path, source, null);

    // Assign the CommandParameter value
    if (parameterValue != null
    {
        if (bindable is Button button)
        {
            button.CommandParameter = parameterValue;
        }
        else if (bindable is ImageButton imgButton)
        {
            imgButton.CommandParameter = parameterValue;
        }
        // And the list goes on ... Definitely not an ideal way to implement this
        else
        {
            // Raise exception as type does not implement commanding
        }
    }

    return bindable;
}
PureWeen commented 2 years ago

Related

https://github.com/xamarin/Xamarin.Forms/pull/2816/files#diff-7dd8666a03d21f3e5147302faab75afb07a27b16e5b8c9d188eda856162d2490R8

egvijayanand commented 2 years ago

Exactly what I'm looking for wrt to the abstraction.

Seems like this has much more benefits than my original intention and this approach addresses another challenge when the name of the Command is different in the case of SearchBar, for example.

Now .NET MAUI engineering team should evaluate this and take it forward.

Regards, Vijay Anand E G

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

egvijayanand commented 1 year ago

@Redth @PureWeen Any possibility of considering this as part of the .NET 8 release?

egvijayanand commented 10 months ago

Can this be taken up into the release scope of .NET 9?