Help me understand the point of commands

I am trying to understand the real world problem that commands solve. The issue might just be that I have not worked on a big enough application in WPF to make proper use of them, but I'm failing to see how they add anything.

The things I have been reading: * Re-use of commands throughout the view. How is this different than re-using a call to a view model function in the view's code behind? I would still have to put that command in the XAML somewhere, so it's not actually saving me a whole lot of typing/copy pasting.

  • Commands can provide support for enabling/disabling many UI elements at once. OK, but so can a property "IsSomePartOfMyViewValid".

  • Commands help to reduce code in a view. I like this one. I wish my views had less code and/or that I didn't have to write clicking handling code at all. However, I still find a one/two liner (if I need a confirmation window) in an event handler to call a view model function to be more straightforward than creating the command and hooking it up.

Confirmations! I see the way to handle showing messages in an MVVM project is to use some sort of dialog service. But now I am putting messages intended to only be used by the UI in my view model/commands. I feel like the view model should only be called into once the view has determined for sure that the view model function should be called. The only way I see this type of thing actually being better than doing confirmations in the view is if you are sharing this command across many views and it is easier to implement your dialog service consumer in each view than it is to re-write the messages.

I get that the point of commands is to adhere strictly to MVVM, but I find that adhering so strictly is very academic, and I'm failing to see any actual real world problem that commands are solving or any actual benefit to using them over events. I guess my big question with the stackoverflow type posts explaining this I've seen is: "OK, you've told me that commands fit into MVVM better than events. I understand, but WHY is that better in the real world than just using the events?" Am I missing some big picture with commands here?

by schlechtums via /r/csharp

Leave a Reply