Search code examples
wpfmvvmcommandcommandparameter

Command and CommandParameter design


Let's say you have a view-model that controls a media player. It has to offer standard control features like Star, Stop, Pause and Resume.

I'm thinking of two ways to design it in the view-model

1st Way : Every action has its own command. - StartCommand - StopCommand - PauseCommand - ResumeCommand

Every button in the view will be bound to it's related command.

2nd way : One single command with different commandparameters. PerformActionCommand and an enumeration that would look like

enum ActionEnum
{
  start, 
  stop,
  pause,
  resume
}

Which way do you think is preferable and for what reasons ?


Solution

  • In this case using separate commands is better. The commands are not very alike, so if you have one command handler, you will have to use a large switch statement inside and probably call separate methods.

    Another reason is that with different commands you easily can have different conditions when those commands are enabled or not - for example Resume is enabled only when the player is paused.