Adam Lee
Adam Lee

Reputation: 25748

what's the major benefit of using command model?

I am wondering what is the major benefit of using command model in WPF. I am comming from Windows Form and MFC, I always made use of writing code in the event handler.

Upvotes: 1

Views: 154

Answers (2)

mohammadAli
mohammadAli

Reputation: 445

Although Commands and Events can be overlapping, they are two different things.
Commands say "do this!", while events say "this just happened!".
For Example: So you might have a "CloseWindowCommand" for closing a window, but the window might have a "ClosingEvent" that tells subscribing objects that is is closing.

Upvotes: 0

dee-see
dee-see

Reputation: 24078

The commands allow for easy management of which UI element should be enabled or not (through the CanExecute member of the ICommand interface).

You can plug the same command in several places or even call a command from another command in a way that is architecturally cleaner, in my opinion, than with events.

With commands you can code tests that are as close as it gets from having a real user clicking in your UI (again with the CanExecute member of the commands you can easily see if command A and its associated controls react as planned to the execution of command B )

On the maintenance side, it's also easier to understand what an application does when reading markup that says <Button Name="SomeButton" Command="{Binding SaveClientDataCommand} /> rather than <Button Name="SomeButton" OnClick="SomeButton_Click" /> when you're dropping in a project you didn't work on previously.

If you use the MVVM pattern, they allow you to move your logic from the View to the ViewModel.

A lot of these points are really about personal preferences and opinions, but that's often the case when there are several ways of doing the same thing. The wheel hasn't been reinvented and there probably isn't anything you can do with commands that was impossible with event-based programming but I feel like it is much cleaner and easier to maintain.

Upvotes: 2

Related Questions