Publish & Subscribe to events using an Event Aggregator
-
Upload
lars-erik-kindblad -
Category
Technology
-
view
1.490 -
download
0
description
Transcript of Publish & Subscribe to events using an Event Aggregator
Publish- and Subscribe to events using an
Event Aggregator
Lars-Erik KindbladSenior Consultant
Blog: kindblad.com
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-subscribeSummary
What is the Publish-Subscribe Pattern?
Publish-Subscribe = Pub/SubMessage/event based patternThe 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
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 busAccepts a message and publishes it out to only those who subscribes for itCan be synchronous or asynchronousMust have
Publish(message) method
Can have Subscribe(subscriber) method Unsubscribe(subscriber) method
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 Coupled Code
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 coupled code
1. Define the messages
2. Publish the messages
3. Subscribe to- and handle the messages
NewCustomerControl
ToolbarControl Event Aggregator
ToolbarControl
NewCustomerControl
Publish:• DisableSave• EnableSave
BtnSave.Enabled= true/false
Subscribe to & handle:• DisableSave• EnableSave
From strong- to loose coupling
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 modification”
The current implementation breaks this principlePublish-Subscribe would not break the principle
RegisterCustomer
EmailCustomer SmsCustomer
Event Aggregator
EmailCustomer SmsCustomer
RegisterCustomer
Publish message:CustomerRegistered
Subscribe to & handle the message
Invoke
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:CustomerRegistered
Subscribe to & handle the message
Invoke
Overview
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
QUESTIONS?
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