Support for generating ICommands from methods #45
Unanswered
PassivePicasso
asked this question in
Ideas
Replies: 1 comment 1 reply
-
I guess one major problem here is that The MVVM toolkit has multiple variant packages for this reason, but that's not really a direction I want to go down with this library. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Before I knew this library was around I started developing my own version of it.
In investigating whether or not I should migrate, as this solution is much more mature than my implementation, I found that there is nothing responding to the need for building ICommands and DependencyProperties/StyledProperties (depending on WPF or Avalonia)
I am curious if Commands is something that would be considered for this library, or if you consider this something that would be a separate library.
I have support in my version of this solution for commands in the form of an AutoCommandAttribute which can take some parameters to handle CanExecute solution. It generates implementations of ICommand as nested classes for the generated type.
For reference this is my version of the Command implementation, I admit relative to this repo its much less rigorously designed.
My overall implementation is fairly barebones and likely has a lot of potential issues that I'm not aware of however it is here:
https://github.com/PassivePicasso/MVVMGenerator/blob/main/MVVMGenerator/Generators/AutoCommandGenerator.cs
Some aspects of note in my implementation;
If a method is virtual, it is possible to override it in a derived class and use the Command generated by the base class to execute it as long as there are no new requirements.
This is nice, however if the derived class needs to add a custom CanExecute, then there will be two nested Command classes with the same name, hiding the parent implementation without the new keyword. This could be officialized by adding the new keyword, but it doesn't feel like the best way to solve that problem.
Nested classes are used to avoid conflicts.
We auto-generate CanExecute for type validation by default, and add a check method when specified in the AutoCommandAttribute declaration
Command classes are public to allow for usage outside the auto-generation, but this isn't necessarily a good setup for a general use library.
Command fields and properties are exposed as ICommand, but its probably better to use the actual class instead of the interface for these properties.
Here is a sample of input/output
Beta Was this translation helpful? Give feedback.
All reactions