0

I'm currently trying to do a MVVM based WPF program using Prism framework and I'm questioning a lot around commands.

Let me introduce you how things are made for the moment: View with some code behind, ViewModel that contains all the data to display and Model that keeps the data persisted.

The question about Commands came when I had to implement an Element that keeps getting the position of the mouse and process some commands to send to a Hardware. For the moment, it's in the code behind. As it's in the code behind, we can encounter the problem that I can not unit test the commands that are sent to the hardware.

Let's say now I want to put this code behind in the ViewModel, the problem will be performance as the UI will react to the position of the mouse constantly wich results in making a lot of get and set to a value (a lot per seconds).

So then I did my searching and it seems that MVVM is the most controversary topic.

Here's an intance: https://stackoverflow.com/a/30343419/10695437 and https://stackoverflow.com/a/1510592/10695437

So therefore, when do we use binding commands ? Is there a specific type of software that it's important or for some not ?

Edit: Marking it as duplicate with a post that I mentioned above made me just say "Woah". It seems like if the question was answered and was controversial 5 years ago can not be reopen to discuss about it. Thank you, next time i'll just stick with what the majority think and not try to get a "real" answer (wich mean an answer that doesn't begin with "I think" or "IMO") around a question.

Selim
  • 37
  • 5
  • "the problem will be performance" - that is just your assumption. There shouldn't be any significant performance difference between invoking an ICommand's Execute handler and a plain method call. – Clemens Dec 18 '18 at 13:52
  • Yes but what i'm thinking is that it has nothing to do in the ViewModel that specific command. I don't send data I just send some value that needs to be processed in real time. Or am I wrong ? – Selim Dec 18 '18 at 14:11
  • Drag handling is one of the things i tend to do in code behind. I often make the mousemove async and use a task.delay to throttle back handling code. – Andy Dec 18 '18 at 18:07
  • @Andy This is what I think aswell. – Selim Dec 19 '18 at 08:02

1 Answers1

0

The big benefit of commands is that you keep the logic for what the command does and whether it can currently be executed in one place. Much better than having Button.IsEnabled values being set scattered throughout your code. This applies doubly so in the MVVM design pattern when the key aim is complete separation between the UI and the rest of your application.

In your particular case, I wouldn't do the update after each individual mouse move message - instead some kind of buffered mechanism (e.g. reactive extensions) so you only update the command after a set period of time, perhaps every half second.

Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • Will I have a delay using your idea ? I need the program to be reactive in real time, even if the mouse has moved from only one pixel to another. – Selim Dec 18 '18 at 13:57
  • @Selim You can have as much or as little delay as you want ... if you're having performance issues then trying to do an update after every single pixel movement is probably overkill. Just try some tests and see what is appropriate for your case. 1/4 or 1/2 second is not going to be that noticeable. – Peregrine Dec 18 '18 at 14:07
  • It is also not ONLY Button.IsEnabled. It may bea button and a menu item and a utton on some hidden configurable elements at the same time, that also are supposed to show the same icon. Buttons control all that in one place. – TomTom Dec 19 '18 at 08:10