Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Publish- and Subscribe to
events using an
Event Aggregator
Lars-Erik Kindblad
Senior Consultant
Blog: kindblad.com
Agenda
What is the Publish-Subscribe Pattern?
 Message
 Publisher
 Subscriber
 Event Aggregator and how to create one...
What is the Publish-Subscribe Pattern?
Publish-Subscribe = Pub/Sub
Message/event based pattern
The publisher creates an...
What is a Message?
A simple .NET POCO class with or without properties
What is a Publisher?
Any class that wants to create and publish a message
What is a Subscriber?
Any class that wants to do something when a specific message or event
occurs
What is an Event Aggregator?
A lightweight message bus
Accepts a message and publishes it out to only those who subscrib...
Event Aggregator for the UI
Event Aggregator for non-UI
Code Sample #1
Name: MainForm
Name: NewCustomerControl
Name: ToolbarControl
New requirement
Save button should be disabled when the first name or lastname is empty
NewCustomerControl
ToolbarControl
BtnSave.Enabled = true/false
Dependency graph
Strongly Coupled Code
Control Control
Control
Control
Control
Control
Control Control
Dependency graph for a more complex scenario
Strongly Coup...
Dependency graph for an enterprise application
Even more strongly coupled code
Event Aggregator
(Pub-Sub)
Control Control
Control
Control
Control
Control
Control Control
A better solution - Loosely cou...
1. Define the messages
2. Publish the messages
3. Subscribe to- and handle the messages
NewCustomerControl
ToolbarControl Event Aggregator
ToolbarControl
NewCustomerControl
Publish:
• DisableSave
• EnableSave
B...
Code Sample #2
New Requirement
The customer should receive an e-mail when the customer has been
registered
1. Create a New Class
2. Call EmailCustomer From RegisterCustomer
New Requirement
Send a SMS to the customer when the customer has been registered
1. Create a New Class
2. Call SmsCustomer From RegisterCustomer
Open Closed Principle
“Software entities (classes, modules, functions, etc.) should be open for
extension, but closed for...
1. Define the Message
2. Create and Publish the Message
3. Subscribe to the Message
RegisterCustomer
EmailCustomer SmsCustomer
Event Aggregator
EmailCustomer SmsCustomer
RegisterCustomer
Publish message:
Cu...
Summary
Publish & Subscribe + Event Aggregator gives:
 Code that is loosely coupled
 Less complex code that is easier t...
QUESTIONS?
The information contained in this presentation is proprietary.
© 2012 Capgemini. All rights reserved.
www.capgemini.com
Ab...
Publish & Subscribe to events using an Event Aggregator
Upcoming SlideShare
Loading in …5
×

of

Publish & Subscribe to events using an Event Aggregator Slide 1 Publish & Subscribe to events using an Event Aggregator Slide 2 Publish & Subscribe to events using an Event Aggregator Slide 3 Publish & Subscribe to events using an Event Aggregator Slide 4 Publish & Subscribe to events using an Event Aggregator Slide 5 Publish & Subscribe to events using an Event Aggregator Slide 6 Publish & Subscribe to events using an Event Aggregator Slide 7 Publish & Subscribe to events using an Event Aggregator Slide 8 Publish & Subscribe to events using an Event Aggregator Slide 9 Publish & Subscribe to events using an Event Aggregator Slide 10 Publish & Subscribe to events using an Event Aggregator Slide 11 Publish & Subscribe to events using an Event Aggregator Slide 12 Publish & Subscribe to events using an Event Aggregator Slide 13 Publish & Subscribe to events using an Event Aggregator Slide 14 Publish & Subscribe to events using an Event Aggregator Slide 15 Publish & Subscribe to events using an Event Aggregator Slide 16 Publish & Subscribe to events using an Event Aggregator Slide 17 Publish & Subscribe to events using an Event Aggregator Slide 18 Publish & Subscribe to events using an Event Aggregator Slide 19 Publish & Subscribe to events using an Event Aggregator Slide 20 Publish & Subscribe to events using an Event Aggregator Slide 21 Publish & Subscribe to events using an Event Aggregator Slide 22 Publish & Subscribe to events using an Event Aggregator Slide 23 Publish & Subscribe to events using an Event Aggregator Slide 24 Publish & Subscribe to events using an Event Aggregator Slide 25 Publish & Subscribe to events using an Event Aggregator Slide 26 Publish & Subscribe to events using an Event Aggregator Slide 27 Publish & Subscribe to events using an Event Aggregator Slide 28 Publish & Subscribe to events using an Event Aggregator Slide 29 Publish & Subscribe to events using an Event Aggregator Slide 30 Publish & Subscribe to events using an Event Aggregator Slide 31 Publish & Subscribe to events using an Event Aggregator Slide 32 Publish & Subscribe to events using an Event Aggregator Slide 33 Publish & Subscribe to events using an Event Aggregator Slide 34 Publish & Subscribe to events using an Event Aggregator Slide 35 Publish & Subscribe to events using an Event Aggregator Slide 36
Upcoming SlideShare
SPOF - Single "Person" of Failure
Next
Download to read offline and view in fullscreen.

1 Like

Share

Download to read offline

Publish & Subscribe to events using an Event Aggregator

Download to read offline

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.

Related Books

Free with a 30 day trial from Scribd

See all

Publish & Subscribe to events using an Event Aggregator

  1. 1. Publish- and Subscribe to events using an Event Aggregator Lars-Erik Kindblad Senior Consultant Blog: kindblad.com
  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 event aggregator (message bus) – but don’t program the messages to be sent directly to any subscribers The subscribers define what messages they want to subscribe to Event Aggregator Subscriber Subscriber Publisher Message MessageMessage Subscribe
  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 event occurs
  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: MainForm Name: NewCustomerControl Name: ToolbarControl
  12. 12. New requirement Save button should be disabled when the first name or lastname is empty
  13. 13. NewCustomerControl ToolbarControl BtnSave.Enabled = true/false Dependency graph Strongly Coupled Code
  14. 14. Control Control Control Control Control Control Control Control Dependency graph for a more complex scenario Strongly Coupled Code
  15. 15. Dependency graph for an enterprise application Even more strongly coupled code
  16. 16. Event Aggregator (Pub-Sub) Control Control Control Control Control Control Control Control A 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. NewCustomerControl ToolbarControl Event Aggregator ToolbarControl NewCustomerControl Publish: • DisableSave • EnableSave BtnSave.Enabled = true/false Subscribe to & handle: • DisableSave • EnableSave From strong- to loose coupling
  21. 21. Code Sample #2
  22. 22. New Requirement The customer should receive an e-mail when the customer has been registered
  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 for extension, but closed for modification” The current implementation breaks this principle Publish-Subscribe would not break the principle RegisterCustomer EmailCustomer SmsCustomer Event Aggregator EmailCustomer SmsCustomer RegisterCustomer Publish message: CustomerRegistered Subscribe to & handle the message Invoke
  29. 29. 1. Define the Message
  30. 30. 2. Create and Publish the Message
  31. 31. 3. Subscribe to the Message
  32. 32. RegisterCustomer EmailCustomer SmsCustomer Event Aggregator EmailCustomer SmsCustomer RegisterCustomer Publish message: CustomerRegistered Subscribe to & handle the message Invoke Overview
  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 layer 2. Other layers when needed How to use it? 1. Download or create your own event aggregator 2. Define a message 3. Create and publish a message 4. Subscribe to the message
  34. 34. QUESTIONS?
  35. 35. The information contained in this presentation is proprietary. © 2012 Capgemini. All rights reserved. www.capgemini.com About Capgemini With more than 120,000 people in 40 countries, Capgemini is one of the world's foremost providers of consulting, technology and outsourcing services. The Group reported 2011 global revenues of EUR 9.7 billion. Together with its clients, Capgemini creates and delivers business and technology solutions that fit their needs and drive the results they want. A deeply multicultural organization, Capgemini has developed its own way of working, the Collaborative Business ExperienceTM, and draws on Rightshore ®, its worldwide delivery model. Rightshore® is a trademark belonging to Capgemini
  • DivyaBaburajan

    Mar. 29, 2014

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.

Views

Total views

3,908

On Slideshare

0

From embeds

0

Number of embeds

8

Actions

Downloads

131

Shares

0

Comments

0

Likes

1

×