Thursday, May 3, 2012

PRISM - Communicating Between Loosely Coupled Components

The Prism Library provides the following communication approaches:  

Solution commanding. Use when there is an expectation of immediate action from the user interaction.
Event aggregation. For communication across view models, presenters, or controllers when there is not a direct action-reaction expectation.
Region context. Use this to provide contextual information between the host and views in the host's region. This approach is somewhat similar to the DataContext, but it does not rely on it.
Shared services. Callers can call a method on the service which raises an event to the receiver of the message. Use this if none of the preceding is applicable.

In short
Composite command- communicate among the views in module and shell project
Delegate command-communicate among the views in the same module
Event aggregation-communicate the views in different modules.
Region context- communicate among the views in the same region
Shared services- Use this if none of the preceding is applicable.

Event Aggregation

Composite Command

You publish an event -> all subscribers receive notice and execute code in response
You execute a composite command -> all registered commands get executed and with it their attached code
Asynchronous
Synchronous

If you need the sending of a message to be initiated by a user gesture such as a button click, Commands may be a good choice. If you need the sending of a message to be initiated by business logic code, such as in a controller or presenter, you should consider using the EventAggregator.
When to use:
There are two primary differences between these two.
  1. CanExecute for Commands. A Command can say whether or not it is valid for execution by calling Command.RaiseCanExecuteChanged() and having its CanExecute delegate return false. If you consider the case of a "Save All" CompositeCommand compositing several "Save" commands, but one of the commands saying that it can't execute, the Save All button will automatically disable (nice!).
  2. EventAggregator is a Messaging pattern and Commands are a Commanding pattern. Although CompositeCommands are not explicitly a UI pattern, it is implicitly so (generally they are hooked up to an input action, like a Button click). EventAggregator is not this way - any part of the application effectively raises an EventAggregator event: background processes, ViewModels, etc. It is a brokered avenue for messaging across your application with support for things like filtering, background thread execution, etc.
It's more difficult to say when to use each, but generally if its user interaction that raises the event use a command, for anything else use EventAggregator.


Solution Commanding

 

 if you need to respond to a user gesture, such as clicking on a command invoker (for example, a button or menu item), and if you want the invoker to be enabled based on business logic, use commanding

DelegateCommand, which allows you to call a delegate method when the command is executed.
The DelegateCommand will be used just to connect the View with its Model.

CompositeCommand, which allows you to combine multiple commands.

How does this help you with cross module communication?
Applications based on the Prism Library may have global CompositeCommands that are defined in the shell that have meaning across modules, such as Save, Save All, and Cancel. Modules can then register their local commands with these global commands and participate in their execution.

example code:
publish:

                        MobileNumber = SearchTextBox.Text;
                        ServiceLocator.Current.GetInstance<IEventAggregator>().GetEvent<SearchCustomerEvent>().Publish(MobileNumber);


subscribe:

  private void Window_Loaded(object sender, RoutedEventArgs e)
        {
                        ServiceLocator.Current.GetInstance<IEventAggregator>().GetEvent<SearchCustomerEvent>().Subscribe(this.Display, true);
        }
          private void Display(object val)
        {
            MessageBox.Show("Mobile number is" + val);

 
        }

Msdn:
FAQ: Questions and answers:
Event Aggregator:
To understand event aggregator:

Simple example using event aggregator



Sample application using event aggregator
Region Context:
Shared Services:


No comments:

Post a Comment