Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the accepted pattern for WPF commanding in MVVM?

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?

like image 370
Robert S. Avatar asked Dec 15 '09 16:12

Robert S.


1 Answers

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.

like image 153
Matthias Avatar answered Sep 28 '22 12:09

Matthias