Tiny messenger
Upcoming SlideShare
Loading in...5
×
 

Tiny messenger

on

  • 1,278 views

 

Statistics

Views

Total Views
1,278
Views on SlideShare
1,278
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Tiny messenger Tiny messenger Presentation Transcript

  • TinyMessenger Event aggregator/messenger for loosely coupled communication
  • Some scenarios Communication between Controllers/ViewModels. Service classes raising "events". Removing responsibilities from classes that receive external events (such as the AppDelegate in an iPhone application). Decoupling communication between classes.
  • How to use? var hub= new TinyMessengerHub(); Or var hub = container.Resolve<ITinyMessengerHub>();
  • How? Publishing //Publishing a message hub.Publish(new MyMessage()); //asyncronously if necessary hub.PublishAsync(new MyMessage()); // callback when publishing is completed hub.PublishAsync(new MyMessage(), MyCallback); // MyCallback is executed on completion
  • How ? Subscribing //Subscribe regardless of how many publishers there are hub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); }); // only receive the message if the content container "Testing" hub.Subscribe<MyMessageAgain>((m) => { MessageBox.Show("Message Received!"); }, (m) => m.Content == "Testing"); //Via proxy hub.Subscribe<MyMessage>((m) => { MessageBox.Show("Received via proxy"); }, DefaultTinyMessageProxy.Instance); // hub.Subscribe<MyMessageAgain>((m) => { MessageBox.Show("Message Received!"); }, (m) => m.Content == "Testing", true, DefaultTinyMessageProxy.Instance);
  • How ? Unsubscribing var token = hub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); }); //Unscubscribing messenger.Unsubscribe<TestMessage>(token ); //other way: token.Dispose()
  • Messages, How to define? //Simplest public class MyMessage : ITinyMessage { public object Sender { get; private set; } } //General public class MyMessageAgain : GenericTinyMessage<String> { public MyMessageAgain(object sender, String content) : base(sender, content) { // We now have a public string property called Content } } //If any subscribers call "Cancel" on our bool isCancelled = false; hub.Publish(new CancellableGenericTinyMessage<string>(this, "Testing", () => { isCancelled = true; }));
  • Proxy, How to define? // This example proxy (for WPF/Silverlight) uses the despatcher to // marshall messages back to the UI thread: public class DispatcherTinyMessageProxy : ITinyMessageProxy { private Dispatcher _Dispatcher; public DispatcherTinyMessageProxy(Dispatcher dispatcher) { _Dispatcher = dispatcher; } public void Deliver(ITinyMessage message, ITinyMessageSubscription subscription) { _Dispatcher.Invoke((Action)delegate { subscription.Deliver(message); }); } }
  • END