SlideShare uma empresa Scribd logo
1 de 9
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

Mais conteúdo relacionado

Semelhante a Tiny messenger

Android notification
Android notificationAndroid notification
Android notification
Krazy Koder
 
Event handling63
Event handling63Event handling63
Event handling63
myrajendra
 

Semelhante a Tiny messenger (7)

Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.
 
Android notification
Android notificationAndroid notification
Android notification
 
Event handling63
Event handling63Event handling63
Event handling63
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
GreenRobot-Eventbus
GreenRobot-EventbusGreenRobot-Eventbus
GreenRobot-Eventbus
 
Web Push Notifications
Web Push NotificationsWeb Push Notifications
Web Push Notifications
 
Eventbus Library and How Does it Work?
Eventbus Library and How Does it Work?Eventbus Library and How Does it Work?
Eventbus Library and How Does it Work?
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 

Tiny messenger

  • 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. How to use? var hub= new TinyMessengerHub(); Or var hub = container.Resolve<ITinyMessengerHub>();
  • 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. 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. How ? Unsubscribing var token = hub.Subscribe<MyMessage>((m) => { MessageBox.Show("Message Received!"); }); //Unscubscribing messenger.Unsubscribe<TestMessage>(token ); //other way: token.Dispose()
  • 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. 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. END