2. What is Dawn
• Dependency Injection library inspired by
Google Guice
• Notification system based on types
• Simple type safe command pattern
3. Why?
• Unhappy with my options
• Small issues that have large consequences
• Flash and Flex
• Small (~12K)
• A new breed of project
4. What its not
• MVC, MVP ... you structure your app
• Plugin architecture
• Aiming to take over the world
5. 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
15. Creating The Doctor
• Construct dependencies within the class
• Service locator
• Factories
• DI By hand
16. Doing it by hand
class TheDoctor{
public var mission:IMission;
public var assistant:IAssistant;
public var tardis:Tardis;
}
17. Doing it by hand
var doctor:TheDoctor = new TheDoctor();
18. Doing it by hand
var tardis:Tardis = new Tardis();
var doctor:TheDoctor = new TheDoctor();
doctor.tardis = tardis;
19. Doing it by hand
var assistant:IAssistant = new RoseTyler();
var tardis:Tardis = new Tardis();
var doctor:TheDoctor = new TheDoctor();
doctor.tardis = tardis;
doctor.assistant = assistant;
20. Doing it by hand
var 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;
21. 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
27. Configure the Doctor
1. Indicate the dependencies
2. Chose an assistant
3. Chose a mission
28. Configure the Doctor
class TheDoctor{
public var mission:IMission;
public var assistant:IAssistant;
public var tardis:Tardis;
}
29. Configure the Doctor
class TheDoctor{
[Inject] public var mission:IMission;
[Inject] public var assistant:IAssistant;
[Inject] public var tardis:Tardis;
}
30. Configure the Doctor
class Config implements IConfig{
public function create(mapper:IMapper):void{
mapper.map(IAssistant).toClass(RoseTyler);
mapper.map(IMission).toClass(Daleks);
}
}
31. Create the Doc
var 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;
34. Create the Doc
var doctor:TheDoctor;
var builder:IBuilder = new Builder(new Config());
doctor = builder.getObject(TheDoctor) as TheDoctor;
35. 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!
36. 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
75. Triggering
class TadPole{
[Inject] public var bus:INotificationBus;
[DependenciesInjected]
public function init():void{
bus.trigger(new TadPoleCreated());
}
}
76. Dawn Notifications
• Simple API
• Use the language, closures are good!
• Types are smarter then strings (Duh!)
• Reduce the complexity of application structure
81. Dawn Commands
class MakeHayCommand{
[Inject] public var barn:Barn;
[Execute] public function execute(note:MakeHay):void{
barn.makeHay(note.howMuchHay);
}
}
82. Dawn Commands
class MakeHayCommand{
[Inject] public var barn:Barn;
[Execute] public function execute(note:MakeHay):void{
barn.makeHay(note.howMuchHay);
}
}
83. Dawn Commands
class MakeHayCommand{
[Inject] public var barn:Barn;
[Execute] public function execute(note:MakeHay):void{
barn.makeHay(note.howMuchHay);
}
}
84. Dawn Commands
class MakeHayCommand{
[Inject] public var barn:Barn;
[Execute] public function execute(note:MakeHay):void{
barn.makeHay(note.howMuchHay);
}
}