Publish & Subscribe to events using an Event Aggregator


Published on

These slides gives guides you through what the Publish-Subscribe pattern is, how to create an Event Aggregator, how you can use it in the UI and in other layers and 2 code samples that refactors from a non pub-sub architecture to a pub-sub architecture.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Publish & Subscribe to events using an Event Aggregator

  1. 1. Publish- and Subscribe toevents using anEvent AggregatorLars-Erik KindbladSenior ConsultantBlog:
  2. 2. AgendaWhat is the Publish-Subscribe Pattern? Message Publisher Subscriber Event Aggregator and how to create oneCode Sample #1 and #2 without and with publish-subscribeSummary
  3. 3. What is the Publish-Subscribe Pattern?Publish-Subscribe = Pub/SubMessage/event based patternThe publisher creates and publishes messages through an eventaggregator (message bus) – but don’t program the messages to be sentdirectly to any subscribersThe subscribers define what messages they want to subscribe toEvent AggregatorSubscriber SubscriberPublisherMessageMessageMessageSubscribe
  4. 4. What is a Message?A simple .NET POCO class with or without properties
  5. 5. What is a Publisher?Any class that wants to create and publish a message
  6. 6. What is a Subscriber?Any class that wants to do something when a specific message or eventoccurs
  7. 7. What is an Event Aggregator?A lightweight message busAccepts a message and publishes it out to only those who subscribes for itCan be synchronous or asynchronousMust have Publish(message) methodCan have Subscribe(subscriber) method Unsubscribe(subscriber) method
  8. 8. Event Aggregator for the UI
  9. 9. Event Aggregator for non-UI
  10. 10. Code Sample #1
  11. 11. Name: MainFormName: NewCustomerControlName: ToolbarControl
  12. 12. New requirementSave button should be disabled when the first name or lastname is empty
  13. 13. NewCustomerControlToolbarControlBtnSave.Enabled = true/falseDependency graphStrongly Coupled Code
  14. 14. Control ControlControlControlControlControlControl ControlDependency graph for a more complex scenarioStrongly Coupled Code
  15. 15. Dependency graph for an enterprise applicationEven more strongly coupled code
  16. 16. Event Aggregator(Pub-Sub)Control ControlControlControlControlControlControl ControlA better solution - Loosely coupled code
  17. 17. 1. Define the messages
  18. 18. 2. Publish the messages
  19. 19. 3. Subscribe to- and handle the messages
  20. 20. NewCustomerControlToolbarControl Event AggregatorToolbarControlNewCustomerControlPublish:• DisableSave• EnableSaveBtnSave.Enabled= true/falseSubscribe to & handle:• DisableSave• EnableSaveFrom strong- to loose coupling
  21. 21. Code Sample #2
  22. 22. New RequirementThe customer should receive an e-mail when the customer has beenregistered
  23. 23. 1. Create a New Class
  24. 24. 2. Call EmailCustomer From RegisterCustomer
  25. 25. New RequirementSend a SMS to the customer when the customer has been registered
  26. 26. 1. Create a New Class
  27. 27. 2. Call SmsCustomer From RegisterCustomer
  28. 28. Open Closed Principle“Software entities (classes, modules, functions, etc.) should be open forextension, but closed for modification”The current implementation breaks this principlePublish-Subscribe would not break the principleRegisterCustomerEmailCustomer SmsCustomerEvent AggregatorEmailCustomer SmsCustomerRegisterCustomerPublish message:CustomerRegisteredSubscribe to &handle themessageInvoke
  29. 29. 1. Define the Message
  30. 30. 2. Create and Publish the Message
  31. 31. 3. Subscribe to the Message
  32. 32. RegisterCustomerEmailCustomer SmsCustomerEvent AggregatorEmailCustomer SmsCustomerRegisterCustomerPublish message:CustomerRegisteredSubscribe to &handle themessageInvokeOverview
  33. 33. SummaryPublish & Subscribe + Event Aggregator gives: Code that is loosely coupled Less complex code that is easier to maintain and unit-testWhere to use it?1. UI/Presentation layer2. Other layers when neededHow to use it?1. Download or create your own event aggregator2. Define a message3. Create and publish a message4. Subscribe to the message
  34. 34. QUESTIONS?
  35. 35. The information contained in this presentation is proprietary.© 2012 Capgemini. All rights reserved.www.capgemini.comAbout CapgeminiWith more than 120,000 people in 40 countries, Capgemini is oneof the worlds foremost providers of consulting, technology andoutsourcing services. The Group reported 2011 global revenuesof EUR 9.7 billion.Together with its clients, Capgemini creates and deliversbusiness and technology solutions that fit their needs and drivethe results they want. A deeply multicultural organization,Capgemini has developed its own way of working, theCollaborative Business ExperienceTM, and draws on Rightshore ®,its worldwide delivery model.Rightshore® is a trademark belonging to Capgemini