dotnet / dotnet/maui

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

Open
#4,396 5 comments 0 reactions 0 assignees View on GitHub
area-architecture proposal/open
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 10h
Merged PRs (30d)
297

Description

### 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:
```CS
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:
```CS
public interface ICommanding
{
ICommand Command { get; }
object CommandParameter { get; }
}
```
```CS
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.

```CS
public static TBindable BindCommandWithParameter(
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;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.