Upload
amir-barylko
View
473
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Presentation done at PRDC West about the Event Aggregator pattern
Citation preview
AMIR BARYLKO
DECOUPLINGWITH THE
EVENT 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!
Amir Barylko - Event Aggregator MavenThought Inc.
RESOURCES
• Email: [email protected]
• Twitter : @abarylko
• Blog: http://www.orthocoders.com
•Materials: http://www.orthocoders.com/presentations
Amir Barylko - Event Aggregator MavenThought Inc.
INTROCouplingCohesion
DependenciesDependency Injection
IoC Containers
Amir Barylko - Event Aggregator MavenThought Inc.
COUPLING & COHESION
Amir Barylko - Event Aggregator MavenThought Inc.
COUPLING(WIKIPEDIA)
Degree to which each program module relies on each one
of the other modules
Amir Barylko - Event Aggregator MavenThought Inc.
COUPLING II
Is usually contrasted with cohesion
Amir Barylko - Event Aggregator MavenThought Inc.
COUPLING III
Invented by Larry Constantine, an original developer of
Structured Design
Amir Barylko - Event Aggregator MavenThought Inc.
COUPLING IV
Low coupling is often a sign of a well-structured computer system and a
good design
Amir Barylko - Event Aggregator MavenThought Inc.
COUPLING V
When combined with high cohesion, supports high
readability and maintainability
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
Amir Barylko - Event Aggregator MavenThought Inc.
IS ALL ABOUTDEPENDENCIES
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!
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;
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?
Amir Barylko - Event Aggregator MavenThought Inc.
INVERSION OF CONTROL
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!
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>());
Amir Barylko - Event Aggregator MavenThought Inc.
REFACTORINGWhat’s wrong?
Event AggregatorDemo
Desktop &Web applications
Amir Barylko - Event Aggregator MavenThought Inc.
WHAT’S WRONG?
Amir Barylko - Event Aggregator MavenThought Inc.
TOO MANY DEPENDENCIES
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?
Amir Barylko - Event Aggregator MavenThought Inc.
DECENTRALIZE
•Identify boundaries
• Identify clear responsibilities
•Reduce complexity
•Find notification mechanism
Amir Barylko - Event Aggregator MavenThought Inc.
Library
WHAT I’D LIKE
Reviews
Posters
????
Amir Barylko - Event Aggregator MavenThought Inc.
EVENT 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
Amir Barylko - Event Aggregator MavenThought Inc.
TRAITS
•Based on subject - observer
•Centralize event registration logic
•No need to track multiple objects
•Level of indirection
Amir Barylko - Event Aggregator MavenThought Inc.
IMPLEMENTATION
Amir Barylko - Event Aggregator MavenThought Inc.
WHAT WE NEED
•Register events
•Raise events
•Subscribe to events
Amir Barylko - Event Aggregator MavenThought Inc.
ATTEMPT #1
Amir Barylko - Event Aggregator MavenThought Inc.
ATTEMPT #2
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!
Amir Barylko - Event Aggregator MavenThought Inc.
RAISING AN EVENT
Amir Barylko - Event Aggregator MavenThought Inc.
HOW TO USE IT
if( !everybody.Asleep() ) {
Demo(); }
Amir Barylko - Event Aggregator MavenThought Inc.
WHAT’S NEXT?
•When a movie is added
•Show notification
•Show posters
•Show reviews
Amir Barylko - Event Aggregator MavenThought Inc.
SUMMARY
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?
Amir Barylko - Event Aggregator MavenThought Inc.
RESOURCES
• Email: [email protected]
• Twitter : @abarylko
• Slides and code: http://bit.ly/orthoslides
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
Amir Barylko - Event Aggregator MavenThought Inc.
SOFTWARE QUALITY WORKSHOP
•When: May 4, 10-11, 16-17
•More info: http://www.maventhought.com