MVVM, Should I use Icommand on all controls?

Asked

Viewed 356 times

4

Part of the MVVM is the use of commands through the interface ICommand. The question is, should I use commands on all controls? Even those that are not connected with the business rule? For example, a HambugerButton whose sole purpose is to expand or collect a HambugerMenu. I must apply Command in it also?

  • 1

    If the action of the button is only related to the view, I don’t see any problem where it is done in the code Behind.

  • If it is only you who will touch the code is even acceptable, the problem is when a lot of people work in the same code then the mess is done, show a certain component to me can be part of the business rule, for another developer not.

  • You don’t need to use all the controls. Icommand abstracts an action within your Viewmodel. And because the viewmodel does not know the View you should not link these actions with, for example, the opening and closing of Hambugermenu. That kind of rule is up to View.

  • Be consistent if you already use ICommands for the other commands continue to use them. Another programmer will find the commands all in the same place and this is good.

1 answer

2


Yes, my opinion is that you should use Commands in all situations, as it maintains the consistency of the standard MVVM and does not cause confusion, because mixing code-behind with MVVM will cause a headache in anyone who moves your code because the features will be mixed in different parts of the code.

For example, look like an implementation MVVM is clean and direct:

Using the example of opening a controller via a button.

// XAML - HamburgerMenu
<controls:HamburgerMenu IsOpen={binding MenuIsOpen} />

The boolean property IsOpen of HambugerMenu was linked to a boolean property of my view-model called MenuIsOpen.

// View-Model
public bool MenuIsOpen { get; private set; } // Implementar INotifyPropertyChanged

You have the button that expands (opens) the HamburgerMenu.

 // XAML - Button
 <controls:Button Command={binding OpenMenu} />

The push button has been connected to ICommand of my view-model called OpenMenu.

// View-Model
public ICommand OpenMenu = new RelayCommand(OpenMenuCommand);

private void OpenMenuCommand()
{
   MenuIsOpen = !MenuIsOpen; // Alterna entre aberto e fechado.
}

Ready, this way you switch between open and closed your Hamburgermenu, no events, no code-Behind, no surprise code.

In the above example, any method within your view-model can control the opening and closing of your hamburgerMenu through property MenuIsOpen which is very good for code reuse because if some routine comes up where you need to control the opening of the control, it is already available to you, which would not be possible with code-Behind (not in a clean way), so you continue in the pattern MVVM, has a clean code, its view contains only the bindings, the view-model doesn’t even know about the controls.

  • 1

    People always comment on what should be used, but you went a step further and explained the practical reasons for using it. High-level response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.