36
Amir Barylko - .NET UG Mar ‘11 MavenThought Inc. AMIR BARYLKO DECOUPLING USING THE EVENT AGGREGATOR .NET USER GROUP MAR 2011 Wednesday, March 30, 2011

decoupling-ea

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

AMIR BARYLKO

DECOUPLINGUSING THE

EVENTAGGREGATOR

.NET USER GROUPMAR 2011

Wednesday, March 30, 2011

Page 2: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

WHO AM I?

• Quality Expert

• Architect

• Developer

• Mentor

• Great cook

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

Wednesday, March 30, 2011

Page 3: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

RESOURCES

• Email: [email protected]

• Twitter : @abarylko

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

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

Wednesday, March 30, 2011

Page 4: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

INTROCouplingCohesion

DependenciesDependency Injection

IoC Containers

Wednesday, March 30, 2011

Page 5: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

COUPLING & COHESION

Wednesday, March 30, 2011

Page 6: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

COUPLING(WIKIPEDIA)

Degree to which each program module relies on each one

of the other modules

Wednesday, March 30, 2011

Page 7: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

COUPLING II

Is usually contrasted with cohesion

Wednesday, March 30, 2011

Page 8: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

COUPLING III

Invented by Larry Constantine, an original developer of

Structured Design

Wednesday, March 30, 2011

Page 9: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

COUPLING IV

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

good design

Wednesday, March 30, 2011

Page 10: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

COUPLING V

When combined with high cohesion, supports high

readability and maintainability

Wednesday, March 30, 2011

Page 11: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

COHESION(WIKIPEDIA)

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

software module is

Wednesday, March 30, 2011

Page 12: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

IS ALL ABOUTDEPENDENCIES

Wednesday, March 30, 2011

Page 13: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

HARDCODEDDEPENDENCIES

public MovieLibrary()

{

this._storage = new LocalStorage();

this._critic = new JaySherman();

this._posterService = new IMDBPosterService();

}

Impossible to test or maintain!

Wednesday, March 30, 2011

Page 14: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

EXTRACT INTERFACES

private JaySherman _critic;

private IMDBPosterService _posterService;

private LocalStorage _storage;

private IMovieCritic _critic;

private IMoviePosterService _posterService;

private IMovieStorage _storage;

Wednesday, March 30, 2011

Page 15: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 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?

Wednesday, March 30, 2011

Page 16: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

INVERSION OF CONTROL

Wednesday, March 30, 2011

Page 17: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

POOR’S MAN DI

public MovieLibrary()

{

this._storage = new LocalStorage();

this._critic = new JaySherman();

this._posterService = new IMDBPosterService();

}

Still testeable...but smells!

Wednesday, March 30, 2011

Page 18: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

USING IOC CONTAINER

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

Wednesday, March 30, 2011

Page 19: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

REFACTORINGWhat’s wrong?

Event AggregatorDemo

Desktop &Web applications

Wednesday, March 30, 2011

Page 20: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

WHAT’S WRONG?

Wednesday, March 30, 2011

Page 21: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

TOO MANY DEPENDENCIES

Wednesday, March 30, 2011

Page 22: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 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?

Wednesday, March 30, 2011

Page 23: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

DECENTRALIZE

• Identify boundaries

• Identify clear responsibilities

•Reduce complexity

•Find notification mechanism

Wednesday, March 30, 2011

Page 24: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

Library

WHAT I’D LIKE

Reviews

Posters

????

Wednesday, March 30, 2011

Page 25: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

EVENT AGGREGATOR

Wednesday, March 30, 2011

Page 26: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 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

Wednesday, March 30, 2011

Page 27: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

TRAITS

•Based on subject - observer

•Centralize event registration logic

•No need to track multiple objects

•Level of indirection

Wednesday, March 30, 2011

Page 28: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

DEMO

Wednesday, March 30, 2011

Page 29: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

WHAT WE NEED

•Register events

•Raise events

•Subscribe to events

Wednesday, March 30, 2011

Page 30: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

IMPLEMENTATION

Wednesday, March 30, 2011

Page 31: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

WHAT’S NEXT?

•Show movies

•Add notification to show posters

•Add notification to show reviews

Wednesday, March 30, 2011

Page 32: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

QUESTIONS?

Wednesday, March 30, 2011

Page 33: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

RESOURCES

• Email: [email protected]

• Twitter : @abarylko

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

• Source Code: https://github.com/amirci/decoupling_mar_11

Wednesday, March 30, 2011

Page 34: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

RESOURCES II

•Coupling: http://en.wikipedia.org/wiki/Coupling_(computer_programming)

•Event Aggregator: http://martinfowler.com/eaaDev/EventAggregator.html

MavenThought Commons:https://github.com/amirci/mt_commons

•Bootstrapper:http://bootstrapper.codeplex.com/

•Windsor Container :http://www.castleproject.org/container/

Wednesday, March 30, 2011

Page 35: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

TDD TRAINING

• When: May 26 & 27

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

• Goal: Learn TDD with real hands on examples

Wednesday, March 30, 2011

Page 36: decoupling-ea

Amir Barylko - .NET UG Mar ‘11 MavenThought Inc.

AGILE USER GROUP

• Check it out! : http://www.agilewinnipeg.com

• Apr 5: Agile Planning

• May: Agile Stories

• Jun: Testing?

Wednesday, March 30, 2011