I'm working on a WPF app and I understand the command pattern pretty well, but I've found that there are several different implementations of the command pattern for MVVM. There's Josh Smith's implementation in his WPF sample app, the DelegateCommand
from Prism, and the CommandBindings
implementation.
My question is, what is the generally accepted best practice for using commands with MVVM? My application uses Prism so DelegateCommand
is available to us.
The devs on my team are arguing about which approach is "best." Some don't like the numerous .cs files generated for each command, others prefer that everything be wired up via CommandBindings
. I'm at a loss. Can anyone shed some light?
Well - I think there isn't the solution.
The CommandBindings are not testable that easy and introduce a dependency to WPF classes in the ViewModel which is not very good. So I wouldn't use them.
Both DelegateCommand and CommandSinkCommand (Josh Smith's solution) are good ways IMO. They are not really different and none of them is superior to the other. Though, I noticed, that the CommandSink version does not work always when command routing gets more complex (especially when DataTemplates are involved).
You can even combine them: Use DelegateCommand and additionally use JoshSmith version - so you can combine the advantages of both. The only thing you need are some helper classes - not really hard to implement.
Much more important is consitency in your application: If you decided what you want to use, you should follow this way through your entire application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With