89
Dawn standing on the shoulders of giants

Dawn - Actionscript Library

  • Upload
    sammyt

  • View
    1.929

  • Download
    0

Embed Size (px)

DESCRIPTION

dependency injection, notifications, type safe commands and actionscript gripes

Citation preview

Page 1: Dawn - Actionscript Library

Dawnstanding on the shoulders of giants

Page 2: Dawn - Actionscript Library

What is Dawn

• Dependency Injection library inspired by Google Guice

• Notification system based on types

• Simple type safe command pattern 

Page 3: Dawn - Actionscript Library

Why?

• Unhappy with my options

• Small issues that have large consequences

• Flash and Flex

• Small (~12K)

• A new breed of project

Page 4: Dawn - Actionscript Library

What its not

• MVC, MVP ... you structure your app

• Plugin architecture

• Aiming to take over the world

Page 5: Dawn - Actionscript Library

Some Design Principles

• Testing is vital

• Play to language strengths

• Type safety is winfull

• Configuration is no fun (keep it DRY)

• Static state is bad

• Types are better than strings

Page 6: Dawn - Actionscript Library

Dependency Injectiona lot of fuss about nothing

Page 7: Dawn - Actionscript Library

TheDoctor

Page 8: Dawn - Actionscript Library

Tardis

TheDoctor

Page 9: Dawn - Actionscript Library

TardisAssistant

TheDoctor

Page 10: Dawn - Actionscript Library

Tardis MissionAssistant

TheDoctor

Page 11: Dawn - Actionscript Library

IAssistant IMissionTardis

TheDoctor

Page 12: Dawn - Actionscript Library

IAssistant IMissionTardis

TheDoctor

RoseTyler

DonnaNoble

Page 13: Dawn - Actionscript Library

IAssistant IMissionTardis

TheDoctor

RoseTyler

DonnaNoble Daleks

Cybermen

Page 14: Dawn - Actionscript Library

IAssistant IMissionTardis

TheDoctor

RoseTyler

DonnaNoble Daleks

Cybermen

Object graph ofTheDoctor

Page 15: Dawn - Actionscript Library

Creating The Doctor• Construct dependencies within the class

• Service locator

• Factories

• DI By hand

Page 16: Dawn - Actionscript Library

Doing it by handclass TheDoctor{ public var mission:IMission; public var assistant:IAssistant; public var tardis:Tardis;}

Page 17: Dawn - Actionscript Library

Doing it by handvar doctor:TheDoctor = new TheDoctor();

Page 18: Dawn - Actionscript Library

Doing it by handvar tardis:Tardis = new Tardis();var doctor:TheDoctor = new TheDoctor();doctor.tardis = tardis;

Page 19: Dawn - Actionscript Library

Doing it by handvar assistant:IAssistant = new RoseTyler();var tardis:Tardis = new Tardis();var doctor:TheDoctor = new TheDoctor();doctor.tardis = tardis;doctor.assistant = assistant;

Page 20: Dawn - Actionscript Library

Doing it by handvar mission:IMission = new Daleks();var assistant:IAssistant = new RoseTyler();var tardis:Tardis = new Tardis();var doctor:TheDoctor = new TheDoctor();doctor.tardis = tardis;doctor.assistant = assistant;doctor.mission = mission;

Page 21: Dawn - Actionscript Library

Doing it by hand• Thats a lot of code to create a Doctor

• More code to maintain

• Fragile to change

• Client of TheDoctor must know inner workings (encapsulation fail)

• Construction followed simple patterns

Page 22: Dawn - Actionscript Library

Dependency Injection is all about creating

object graphs!

Page 23: Dawn - Actionscript Library

Dawn DI• How can object creation be automated and easy?

• Meets design principles

• Thank you Google Guice!

Page 24: Dawn - Actionscript Library

Configure the Doctor

Page 25: Dawn - Actionscript Library

Configure the Doctor1. Indicate the dependencies

Page 26: Dawn - Actionscript Library

Configure the Doctor1. Indicate the dependencies

2. Chose an assistant

Page 27: Dawn - Actionscript Library

Configure the Doctor1. Indicate the dependencies

2. Chose an assistant

3. Chose a mission

Page 28: Dawn - Actionscript Library

Configure the Doctorclass TheDoctor{ public var mission:IMission; public var assistant:IAssistant; public var tardis:Tardis;}

Page 29: Dawn - Actionscript Library

Configure the Doctorclass TheDoctor{ [Inject] public var mission:IMission; [Inject] public var assistant:IAssistant; [Inject] public var tardis:Tardis;}

Page 30: Dawn - Actionscript Library

Configure the Doctorclass Config implements IConfig{ public function create(mapper:IMapper):void{

mapper.map(IAssistant).toClass(RoseTyler);

mapper.map(IMission).toClass(Daleks);

}}

Page 31: Dawn - Actionscript Library

Create the Docvar mission:IMission = new Daleks();var assistant:IAssistant = new RoseTyler();var tardis:Tardis = new Tardis();var doctor:TheDoctor = new TheDoctor();doctor.tardis = tardis;doctor.assistant = assistant;doctor.mission = mission;

Page 32: Dawn - Actionscript Library

Create the Doc

Page 33: Dawn - Actionscript Library

Create the Doc

builder.getObject(TheDoctor)

Page 34: Dawn - Actionscript Library

Create the Doc

var doctor:TheDoctor;var builder:IBuilder = new Builder(new Config());

doctor = builder.getObject(TheDoctor) as TheDoctor;

Page 35: Dawn - Actionscript Library

Dawn DI• You write less code (no XML)

• Configuration is ActionScript (and small)

• Classes are compiled into final swf

• Agile code, refactor structure fast and easily

• Testing is easy, second nature

• DI helps you write better code!

• Dawn makes DI easy!

Page 36: Dawn - Actionscript Library

Dawn DI• Scopes - Singleton or Transient

• Map toFactory/toInstance for construction control

• Modular configuration

• Name injections

• Inject properties like strings or arrays

• Optional injections

• Inject via mutators or properties

Page 37: Dawn - Actionscript Library

FP-183

Page 38: Dawn - Actionscript Library

[Inject] Dawn DI• Simple

• Light

• Type safe

• Test friendly

• Terse

Page 39: Dawn - Actionscript Library

Notificationsdecoupling without the compromise

Page 40: Dawn - Actionscript Library

Dawn Notifications

• Type based

• Closure friendly

• Static free

Page 41: Dawn - Actionscript Library

The Problem

Page 42: Dawn - Actionscript Library

The Problem

View

View

View

View

Page 43: Dawn - Actionscript Library

The Problem

View

View

View

View

Data

Page 44: Dawn - Actionscript Library

The Problem

View

View

View

View

DataData

Page 45: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataData

Page 46: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 47: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 48: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 49: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 50: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 51: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 52: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 53: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 54: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 55: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Page 56: Dawn - Actionscript Library

The Problem

View

View

View

View

DataDataDataData

Gets a little confusing?

Page 57: Dawn - Actionscript Library

Decouple and Scale

Page 58: Dawn - Actionscript Library

Decouple and Scale

View

View

View

View

Page 59: Dawn - Actionscript Library

Decouple and Scale

View

View

View

View

DataDataDataData

Page 60: Dawn - Actionscript Library

Decouple and ScaleNotification System

View

View

View

View

DataDataDataData

Page 61: Dawn - Actionscript Library

Decouple and ScaleNotification System

View

View

View

View

DataDataDataData

Page 62: Dawn - Actionscript Library

Decouple and ScaleNotification System

View

View

View

View

DataDataDataData

Page 63: Dawn - Actionscript Library

Type based

Page 64: Dawn - Actionscript Library

Type based

addEventListener(type:String,closure:Function...)

Page 65: Dawn - Actionscript Library

Type based

addEventListener(type:String,closure:Function...)

addCallback(type:Class,closure:Function);

Page 66: Dawn - Actionscript Library

Why Types

Page 67: Dawn - Actionscript Library

Why Types• Make full use of polymorphism

IEventType, BaseRpcEvent, com.wibble.Woo

Page 68: Dawn - Actionscript Library

Why Types• Make full use of polymorphism

IEventType, BaseRpcEvent, com.wibble.Woo

• Avoids meaningless staticsFinishedBaconEvent.FINISHED_BACON

Page 69: Dawn - Actionscript Library

Why Types• Make full use of polymorphism

IEventType, BaseRpcEvent, com.wibble.Woo

• Avoids meaningless staticsFinishedBaconEvent.FINISHED_BACON

• No hidden collisions MyEvent.CLOSE == Event.CLOSE == FAIL

Page 70: Dawn - Actionscript Library

Why Types• Make full use of polymorphism

IEventType, BaseRpcEvent, com.wibble.Woo

• Avoids meaningless staticsFinishedBaconEvent.FINISHED_BACON

• No hidden collisions MyEvent.CLOSE == Event.CLOSE == FAIL

• No switch statementsswitch( notification.getName() )

Page 71: Dawn - Actionscript Library

Closure unfriendly

addEventListener(Event.ADDED, function(event:Event):void{ trace(“haha, Try removing me”) });

Page 72: Dawn - Actionscript Library

Closure friendly!

Page 73: Dawn - Actionscript Library

Closure friendly!

var listener:IListenerRegistration = bus.addCallback(ILogAction, function(note:ILogAction):void{ } );

Page 74: Dawn - Actionscript Library

Closure friendly!

var listener:IListenerRegistration = bus.addCallback(ILogAction, function(note:ILogAction):void{ } );

listener.remove();

Page 75: Dawn - Actionscript Library

Triggering

class TadPole{[Inject] public var bus:INotificationBus;

[DependenciesInjected]public function init():void{

bus.trigger(new TadPoleCreated());

}}

Page 76: Dawn - Actionscript Library

Dawn Notifications• Simple API

• Use the language, closures are good!

• Types are smarter then strings (Duh!)

• Reduce the complexity of application structure

Page 77: Dawn - Actionscript Library

Commandssimple tools are easy to build with

Page 78: Dawn - Actionscript Library

Commands

• easy to maintain stateless goodies

• central point for lots of easy wins

• queueing

• cacheing

• handle errors

• etc etc.

Page 79: Dawn - Actionscript Library

Commands

• ICommand interfaces force casting

• execute(param:Object):void

• How to access other application objects

• Model.getInstance();

• Mapping Strings to Commands is fiddly (more developer discipline)

• registerCommand(Notify.GET_THING, GetThingCommand);

Page 80: Dawn - Actionscript Library

Dawn Commands

• Injectable

• Type safe

• Type based DRY configuration

Page 81: Dawn - Actionscript Library

Dawn Commandsclass MakeHayCommand{

[Inject] public var barn:Barn;

[Execute] public function execute(note:MakeHay):void{

barn.makeHay(note.howMuchHay);

}

}

Page 82: Dawn - Actionscript Library

Dawn Commandsclass MakeHayCommand{

[Inject] public var barn:Barn;

[Execute] public function execute(note:MakeHay):void{

barn.makeHay(note.howMuchHay);

}

}

Page 83: Dawn - Actionscript Library

Dawn Commandsclass MakeHayCommand{

[Inject] public var barn:Barn;

[Execute] public function execute(note:MakeHay):void{

barn.makeHay(note.howMuchHay);

}

}

Page 84: Dawn - Actionscript Library

Dawn Commandsclass MakeHayCommand{

[Inject] public var barn:Barn;

[Execute] public function execute(note:MakeHay):void{

barn.makeHay(note.howMuchHay);

}

}

Page 85: Dawn - Actionscript Library

Dawn Commands

addCommand(MakeHayCommand);

Page 86: Dawn - Actionscript Library

Dawn

• Dependency Injection

• Notifications

• Commands

Page 87: Dawn - Actionscript Library
Page 89: Dawn - Actionscript Library

Questions etc?