40
AMIR BARYLKO DECOUPLING WITH THE EVENT AGGREGATOR

PRDCW-avent-aggregator

Embed Size (px)

DESCRIPTION

Presentation done at PRDC West about the Event Aggregator pattern

Citation preview

Page 1: PRDCW-avent-aggregator

AMIR BARYLKO

DECOUPLINGWITH THE

EVENT AGGREGATOR

Page 2: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

WHO AM I?

•Quality Expert

• Architect

•Developer

•Mentor

• Great cook

• The one who’s entertaining you for the next hour!

Page 3: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

RESOURCES

• Email: [email protected]

• Twitter : @abarylko

• Blog: http://www.orthocoders.com

•Materials: http://www.orthocoders.com/presentations

Page 4: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

INTROCouplingCohesion

DependenciesDependency Injection

IoC Containers

Page 5: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

COUPLING & COHESION

Page 6: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

COUPLING(WIKIPEDIA)

Degree to which each program module relies on each one

of the other modules

Page 7: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

COUPLING II

Is usually contrasted with cohesion

Page 8: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

COUPLING III

Invented by Larry Constantine, an original developer of

Structured Design

Page 9: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

COUPLING IV

Low coupling is often a sign of a well-structured computer system and a

good design

Page 10: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

COUPLING V

When combined with high cohesion, supports high

readability and maintainability

Page 11: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

COHESION(WIKIPEDIA)

measure of how strongly-related the functionality expressed by the source code of a

software module is

Page 12: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

IS ALL ABOUTDEPENDENCIES

Page 13: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

HARDCODEDDEPENDENCIES

public MovieLibrary()

{

this._storage = new LocalStorage();

this._critic = new JaySherman();

this._posterService = new IMDBPosterService();

}

very hard to test and maintain!

Page 14: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

EXTRACT INTERFACES

private JaySherman _critic;

private IMDBPosterService _posterService;

private LocalStorage _storage;

private IMovieCritic _critic;

private IMoviePosterService _posterService;

private IMovieStorage _storage;

Page 15: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

DEPENDENCY INJECTION

public MovieLibrary(IMovieStorage storage,

IMovieCritic critic,

IMoviePosterService posterService)

{

this._storage = storage;

this._critic = critic;

this._posterService = posterService;

}

Better for testing... but who is going to initialize them?

Page 16: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

INVERSION OF CONTROL

Page 17: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

POOR’S MAN DI

public MovieLibrary()

{

this._storage = new LocalStorage();

this._critic = new JaySherman();

this._posterService = new IMDBPosterService();

}

Still testeable...but smells!

Page 18: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

USING IOC CONTAINER

Container.Register( Component .For<IMovieCritic>() .ImplementedBy<JaySherman>(), Component .For<IMoviePosterService>() .ImplementedBy<IMDBPosterService>(), Component .For<IMovieStorage>() .ImplementedBy<LocalStorage>());

Page 19: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

REFACTORINGWhat’s wrong?

Event AggregatorDemo

Desktop &Web applications

Page 20: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

WHAT’S WRONG?

Page 21: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

TOO MANY DEPENDENCIES

Page 22: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

LET’S THINK

•Why the critic has to know the library (or viceversa)?

•Or the poster service?

• If I need more services, do I add more dependencies to the library?

Page 23: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

DECENTRALIZE

•Identify boundaries

• Identify clear responsibilities

•Reduce complexity

•Find notification mechanism

Page 24: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

Library

WHAT I’D LIKE

Reviews

Posters

????

Page 25: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

EVENT AGGREGATOR

Page 26: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

THE PATTERN

Channel events from multiple o b j e c t s i n t o a single object to s i m p l i f y registration for clients

Page 27: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

TRAITS

•Based on subject - observer

•Centralize event registration logic

•No need to track multiple objects

•Level of indirection

Page 28: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

IMPLEMENTATION

Page 29: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

WHAT WE NEED

•Register events

•Raise events

•Subscribe to events

Page 30: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

ATTEMPT #1

Page 31: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

ATTEMPT #2

Page 32: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

WHAT?NO CONCRETE EVENT?

• How can we get a concrete from an interface?

• Castle Dynamic Proxy!

• I have an interface and get a Proxy to a stub

• All properties are stubbed to be configured!

Page 33: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

RAISING AN EVENT

Page 34: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

HOW TO USE IT

if( !everybody.Asleep() ) {

Demo(); }

Page 35: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

WHAT’S NEXT?

•When a movie is added

•Show notification

•Show posters

•Show reviews

Page 36: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

SUMMARY

Page 37: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

TO FINALIZE

•Why EA reduces complexity?

•What’s the difference with Event Sourcing?

• So, would that mean we are doing CQRS?

• How would it work with web applications?

•What about WeakReferences?

•What about polymorphic event handling?

Page 38: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

RESOURCES

• Email: [email protected]

• Twitter : @abarylko

• Slides and code: http://bit.ly/orthoslides

Page 39: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

RESOURCES II

•Coupling: http://bit.ly/yo5AK7

•Event Aggregator: http://bit.ly/zL1LrG

•MavenThought Commons: http://bit.ly/mt_commons

•Bootstrapper:http://bit.ly/xHNiKB

•Windsor Container :http://bit.ly/AmodqG

•Castle Dynamic Proxy: http://bit.ly/wihfid

Page 40: PRDCW-avent-aggregator

Amir Barylko - Event Aggregator MavenThought Inc.

SOFTWARE QUALITY WORKSHOP

•When: May 4, 10-11, 16-17

•More info: http://www.maventhought.com