views:

185

answers:

2

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?

+3  A: 

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.

winSharp93
+2  A: 

Two points to consider:

The commands provided by different MVVM frameworks, such as the CommandSinkCommand or SimpleCommand (from Cinch), etc. work as normal ICommands do. In particular, the CanExecute handler is evaluated whenever the WPF believes something has happened which could cause an UI change. You can also manually force this through CommandManager.InvalidateRequerySuggested().

In contrast to that, the DelegateCommands<> of Prism do not call the CanExecute handler unless you manually call RaiseCanExecuteChanged() on the command. If you wish to change this so it also gets fired when the commands are normally requeried, see the code at http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=47338

EDIT:

Second point: The commands of MVVM frameworks usually accept an object as command parameter. In contrast, the DelegateCommands<> of Prism are more strongly typed (though you could of course have a DelegateCommands if you want to).

Daniel Rose
I switched the accepted answer to yours because of the excellent second paragraph. Thanks for the input!
Robert S.
Also check out MVVMLights RelayCommand which is very similar to the Delegate Command.
Agies