Publish & Subscribe to events using an Event Aggregator

36
Publish- and Subscribe to events using an Event Aggregator Lars-Erik Kindblad Senior Consultant Blog: kindblad.com

description

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.

Transcript of Publish & Subscribe to events using an Event Aggregator

Page 1: Publish & Subscribe to events using an Event Aggregator

Publish- and Subscribe to events using an

Event Aggregator

Lars-Erik KindbladSenior Consultant

Blog: kindblad.com

Page 2: Publish & Subscribe to events using an Event Aggregator

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

Page 3: Publish & Subscribe to events using an Event Aggregator

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

Page 4: Publish & Subscribe to events using an Event Aggregator

What is a Message?

A simple .NET POCO class with or without properties

Page 5: Publish & Subscribe to events using an Event Aggregator

What is a Publisher?

Any class that wants to create and publish a message

Page 6: Publish & Subscribe to events using an Event Aggregator

What is a Subscriber?

Any class that wants to do something when a specific message or event occurs

Page 7: Publish & Subscribe to events using an Event Aggregator

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

Page 8: Publish & Subscribe to events using an Event Aggregator

Event Aggregator for the UI

Page 9: Publish & Subscribe to events using an Event Aggregator

Event Aggregator for non-UI

Page 10: Publish & Subscribe to events using an Event Aggregator

Code Sample #1

Page 11: Publish & Subscribe to events using an Event Aggregator

Name: MainForm

Name: NewCustomerControl

Name: ToolbarControl

Page 12: Publish & Subscribe to events using an Event Aggregator

New requirement

Save button should be disabled when the first name or lastname is empty

Page 13: Publish & Subscribe to events using an Event Aggregator
Page 14: Publish & Subscribe to events using an Event Aggregator

NewCustomerControl

ToolbarControl

BtnSave.Enabled = true/false

Dependency graph

Strongly Coupled Code

Page 15: Publish & Subscribe to events using an Event Aggregator

Control Control

Control

Control

Control

Control

Control Control

Dependency graph for a more complex scenario

Strongly Coupled Code

Page 16: Publish & Subscribe to events using an Event Aggregator

Dependency graph for an enterprise application

Even more strongly coupled code

Page 17: Publish & Subscribe to events using an Event Aggregator

Event Aggregator (Pub-Sub)

Control Control

Control

Control

Control

Control

Control Control

A better solution - Loosely coupled code

Page 18: Publish & Subscribe to events using an Event Aggregator

1. Define the messages

Page 19: Publish & Subscribe to events using an Event Aggregator

2. Publish the messages

Page 20: Publish & Subscribe to events using an Event Aggregator

3. Subscribe to- and handle the messages

Page 21: Publish & Subscribe to events using an Event Aggregator

NewCustomerControl

ToolbarControl Event Aggregator

ToolbarControl

NewCustomerControl

Publish:• DisableSave• EnableSave

BtnSave.Enabled= true/false

Subscribe to & handle:• DisableSave• EnableSave

From strong- to loose coupling

Page 22: Publish & Subscribe to events using an Event Aggregator

Code Sample #2

Page 23: Publish & Subscribe to events using an Event Aggregator

New Requirement

The customer should receive an e-mail when the customer has been registered

Page 24: Publish & Subscribe to events using an Event Aggregator

1. Create a New Class

Page 25: Publish & Subscribe to events using an Event Aggregator

2. Call EmailCustomer From RegisterCustomer

Page 26: Publish & Subscribe to events using an Event Aggregator

New Requirement

Send a SMS to the customer when the customer has been registered

Page 27: Publish & Subscribe to events using an Event Aggregator

1. Create a New Class

Page 28: Publish & Subscribe to events using an Event Aggregator

2. Call SmsCustomer From RegisterCustomer

Page 29: Publish & Subscribe to events using an Event Aggregator

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

Page 30: Publish & Subscribe to events using an Event Aggregator

1. Define the Message

Page 31: Publish & Subscribe to events using an Event Aggregator

2. Create and Publish the Message

Page 32: Publish & Subscribe to events using an Event Aggregator

3. Subscribe to the Message

Page 33: Publish & Subscribe to events using an Event Aggregator

RegisterCustomer

EmailCustomer SmsCustomer

Event Aggregator

EmailCustomer SmsCustomer

RegisterCustomer

Publish message:CustomerRegistered

Subscribe to & handle the message

Invoke

Overview

Page 34: Publish & Subscribe to events using an Event Aggregator

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

Page 35: Publish & Subscribe to events using an Event Aggregator

QUESTIONS?

Page 36: Publish & Subscribe to events using an Event Aggregator

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