WPF: MVVM: Command vs CallMethodAction?
Asked Answered
S

2

6

I'm learning the MVVM pattern with a new(small) project, and I've one question about the way to invoke actions on our controller:

I saw many tutorial where they were telling us to use Command, implying to declare a RelayCommand, initialize it and create the action called by the RelayCommand.

In the other side, I've a colleague which said me that I can use the CallMethodAction with a trigger:

<i:Interaction.Triggers> 
  <i:EventTrigger> 
    <ei:CallMethodAction MethodName="Init" TargetObject="{Binding}" /> 
  </i:EventTrigger> 
</i:Interaction.Triggers> 

For me, his approach has the advantage that I don't have to make some inits methods for commands(which may be never used).

So what am I missing? Why everybody use commands?

Seamaid answered 27/11, 2012 at 9:58 Comment(0)
C
4

Commands provide functionality for disabling in ViewModel code. That can be used to automatically disable e.g buttons bound to command. That's what makes Commands better. Besides, based on your logic you could just dynamically pluck another Command in the same slot and it will reroute the traffic from View, whereas in CallMethodAction you would have to write the rerouting logic in the called method, which would be ugly.

As you can see, it depends on what you try to accomplish and how complicated is your logic :)

Crossfertilize answered 27/11, 2012 at 10:1 Comment(3)
Ok, you're talking about the "canExecute" I guess? I didn't tought of that. But none of the two solution is wrong in the MVVM context?Seamaid
I don't know if I would say, that CallMethodAction is wrong, but... look at it from readability and maintainability point of view: when you have a Command property on your VM it shows everyone that this is the point where the flow from View comes in. If you use CallMethodAction, then it deteriorates the maintainability. In my opinion (and note that it's just an opinion) CallMethodAction should be used sparsly, and RelayCommand should be the default way to interact with VM. The problem is just one: there's no ExecuteCommand Action in Blend SDK, but it can be easily written.Aztec
Your answer very useful for me, but I didn't understand the following line "Command in the same slot and it will reroute the traffic from View, whereas in CallMethodAction you would have to write the rerouting logic"...Can you explain me with an example @Paweł MotylTocopherol
A
7

Commands are out-of-the-box solution and can be attached only to elements which implement the ICommand interface. On the other hand, event triggers can be attached to any event, what makes them more flexible. I follow the general strategy to use Commands where user interactions are involved (buttons, menus) and CanExecute pattern is needed. My commands are strictly connected to visual interface (provide caption, image source, etc.). I use CallMethodAction in any other situation when I want to get rid of code-behind.

Ambidexter answered 27/11, 2012 at 10:37 Comment(0)
C
4

Commands provide functionality for disabling in ViewModel code. That can be used to automatically disable e.g buttons bound to command. That's what makes Commands better. Besides, based on your logic you could just dynamically pluck another Command in the same slot and it will reroute the traffic from View, whereas in CallMethodAction you would have to write the rerouting logic in the called method, which would be ugly.

As you can see, it depends on what you try to accomplish and how complicated is your logic :)

Crossfertilize answered 27/11, 2012 at 10:1 Comment(3)
Ok, you're talking about the "canExecute" I guess? I didn't tought of that. But none of the two solution is wrong in the MVVM context?Seamaid
I don't know if I would say, that CallMethodAction is wrong, but... look at it from readability and maintainability point of view: when you have a Command property on your VM it shows everyone that this is the point where the flow from View comes in. If you use CallMethodAction, then it deteriorates the maintainability. In my opinion (and note that it's just an opinion) CallMethodAction should be used sparsly, and RelayCommand should be the default way to interact with VM. The problem is just one: there's no ExecuteCommand Action in Blend SDK, but it can be easily written.Aztec
Your answer very useful for me, but I didn't understand the following line "Command in the same slot and it will reroute the traffic from View, whereas in CallMethodAction you would have to write the rerouting logic"...Can you explain me with an example @Paweł MotylTocopherol

© 2022 - 2024 — McMap. All rights reserved.