TinyMessenger
Event
aggregator/messenger
for loosely coupled
communication
Some scenarios
Communication between Controllers/ViewModels.
Service classes raising "events".
Removing responsibilities f...
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 MyM...
How ? Subscribing
//Subscribe regardless of how many publishers there are
hub.Subscribe<MyMessage>((m) => { MessageBox.Sho...
How ? Unsubscribing
var token = hub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message
Received!"); });
//Unscubscribi...
Messages, How to define?
//Simplest
public class MyMessage : ITinyMessage
{
public object Sender { get; private set; }
}
/...
Proxy, How to define?
// This example proxy (for WPF/Silverlight) uses the despatcher to // marshall messages
back to the ...
END
Upcoming SlideShare
Loading in …5
×

Tiny messenger

1,886 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,886
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Tiny messenger

  1. 1. TinyMessenger Event aggregator/messenger for loosely coupled communication
  2. 2. 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.
  3. 3. How to use? var hub= new TinyMessengerHub(); Or var hub = container.Resolve<ITinyMessengerHub>();
  4. 4. 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
  5. 5. 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);
  6. 6. How ? Unsubscribing var token = hub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); }); //Unscubscribing messenger.Unsubscribe<TestMessage>(token ); //other way: token.Dispose()
  7. 7. 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; }));
  8. 8. 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); }); } }
  9. 9. END

×