Commands vs Events - what's the difference?

Topics: CAB & Smart Client Software Factory
Jan 8, 2007 at 6:51 PM
I'm not sure I really understand why there are both Commands and Events in the CAB. What's the difference between these two mechanisms? And when should you use one versus the other?

Is it that with Commands you can hook up one handler to many UIElements. Whereas with Events you can hook up many handlers to the same event and/or same UIElement?

In other words,
Command: multiple publishers, one subscriber
Events: one publisher, multiple subscribers

Any feedback would be appreciated... I'm just trying to userstand the model...
Jan 8, 2007 at 8:54 PM
The basic difference is use. Commands were designed to be coupled to UIExtensionSite elements, like MenuItems and ToolStrip items. EventBroker events have a less narrow use-case; you can use them for cross-module communication, cross-workItem communication, etc., and you can pass typed arguments. Commands are limited to EventArgs.

Commands are just there to let you know when a toolstrip or menustrip item was clicked. At least that's how I view it.
Jan 9, 2007 at 2:39 PM
"...At least that's how I view it."

Me too, Chris.

Using a Command is like telling CAB "hey, I want you to do this."

Using loosely Coupled Events is like saying "hey CAB, do this and--btw--here's an extra argument you can work with."

Commands also have a "Status" Property which can be used to enable/disable/hide menu items via CAB. However, the state of any other properties you might use on a menu item (e.g. checked) will have to be stored via member variables.
Jan 9, 2007 at 9:26 PM
I think more generally, commands are actor-initiated events, which begin a use case. Consider, for example, binding a command to a command-line invocation, or the application of a security policy change giving the user more priviliges (like UAC does, in effect), or the change in the network status, issuing a command to go online or offline. All of these are actor-initiated events, rather than internal, program state-change notifications, and can be exposed via a menu item, toolbar item, command text string or message. Of course, you have to create your own bindings for all but the menu and toolbar items.