SlideShare a Scribd company logo
1 of 11
Download to read offline
Table of Contents
Table of Contents........................................................................................................................................ i
Instructors Biographies.............................................................................................................................. ii
Agenda...................................................................................................................................................... iii
Objectives Of the Course.......................................................................................................................... iv
1Setting Up Environment...........................................................................................................................6
   1.1Setting up Mercurial.........................................................................................................................6
   1.2Setting up Your Favorite Text Editor or Eclipse...............................................................................6
   1.3Getting the Source............................................................................................................................ 6
   1.4Setting up Test Runner..................................................................................................................... 8
2Understanding Manual Dependency Injection.......................................................................................10
   2.1Application wiring diagram............................................................................................................10
   2.2Understanding the Need for DI...................................................................................................... 10
3Designing Automated Dependency Injection Framework..................................................................... 12
   3.1Replacing the Lost Information Of Declared Types.......................................................................12
   3.2Ability to Inject Types.................................................................................................................... 12
   3.3Ability to Inject DOM Elements.................................................................................................... 14
   3.4Ability to Inject Events...................................................................................................................14
4Implementing the Framework................................................................................................................ 16
5Comparing the Resulting Wiring Code.................................................................................................. 18




OOPSLA 2009 Tutorial                                                           i                                                         Miško Hevery
Instructors Biographies

Miško Hevery
misko@hevery.com
Google, Inc.


… works as an Agile Coach at Google where he is responsible for coaching Googlers to maintain the
high level of automated testing culture. This allows Google to do frequent releases of its web
applications with consistent high quality. Previously he worked at Adobe, Sun Microsystems, Intel, and
Xerox (to name a few), where he became an expert in building web applications in web related
technologies such as Java, JavaScript, Flex and ActionScript. He is very involved in Open Source
community and an author of several open source projects. Recently his interest in Test-Driven-
Development turned into Testability Explorer (http://code.google.com/p/testability-explorer) and
JsTestDriver (http://code.google.com/p/js-test-driver) with which he hopes to change the testing culture
of the open source community.




OOPSLA 2009 Tutorial                               ii                                     Miško Hevery
Agenda
   1. Setting up your environment
      1. Setting up Mercurial
      2. Setting up your favorite text editor or Eclipse
      3. Setting up JsTestRunner
   2. Getting the source
      1. Checking out the source code
      2. Running the code
   3. Understanding manual Dependency Injection
      1. Application wiring diagram
      2. Understanding the need for DI
   4. Designing automated Dependency Injection framework
      1. Replacing the lost information of declared types
      2. Injecting types
      3. Injecting DOM elements
      4. Injecting events
   5. Implementing the framework
   6. Cleaning up the bootstrap wiring code
   7. Discussion of before and after code




OOPSLA 2009 Tutorial                              iii       Miško Hevery
Objectives of the Course
Dependency Injection is a good practice even in dynamic languages. Because many dynamic languages
do not have type declarations, building an automatic DI framework presents its own set of challenges.
Furthermore, many concepts, such as scopes that are often associated with threads, change their
meaning in most dynamic languages which are single threaded. In this tutorial, we will build a simple
automatic DI framework and solve many of the challenges which are associated with lack of declared
types in such languages.

   •   Understand the need for Dependency Injection
       ◦ Dynamic languages are not special and still need Dependency Injection
   •   How to replace the lack of typing information in dynamic languages
   •   Injection is not limited to injecting object instances
       ◦ Building our own domain specific language
       ◦ The obvious: Injecting types
       ◦ Thinking outside the box: Injecting DOM elements, and events
   •   Dependency Injection simplifies the wiring of the application logic
       ◦ Comparing application bootstrap process before and after




OOPSLA 2009 Tutorial                             iv                                    Miško Hevery
1 Setting Up Environment
Before the tutorial we recommend that you complete all of the following steps:

1.1 Setting up Mercurial
The code for this project is located at:
   •   http://bitbucket.org/misko/misko-hevery-oopsla-09
You will need to install Mercurial for your platform:
   •   Home: http://mercurial.selenic.com/wiki/
   •   Download: http://mercurial.selenic.com/wiki/Download?
       action=show&redirect=BinaryPackages

1.2 Getting the Source
After installing a copy of Mercurial you can download the source using the following command:
   $ hg clone https://misko@bitbucket.org/misko/misko-hevery-oopsla-09/


1.3 Setting up Your Favorite Text Editor or Eclipse
You will need you favorite Text Editor for editing JavaScript, HTML, and CSS. We recommend Eclipse
which you can download and install from the link bellow. We have already set up an Eclipse workspace
for you in the Mercurial repository.
   •   Download Eclipse: http://www.eclipse.org/downloads/
   •   Install JavaScript Editor: From Eclipse
       ◦ Help → Install New Software
       ◦ Select: Work With: Galileo
       ◦ Select: Web, XML, and Java EE Development
       ◦ Complete the installation by clicking the “Next” button
   •   Install JsTestDriver for Eclipse
       ◦ Help → Install New Software
       ◦ Select: Work With: and enter: http://js-test-driver.googlecode.com/svn/update/
       ◦ Select JsTestDriver
       ◦ Complete the installation by clicking the “Next” button
   •   Import the DI project which you downloaded with Mercurial.




OOPSLA 2009 Tutorial                                                                      Miško Hevery
1.4 Setting up Test Runner
NOTE: All commands need to be executed from the root of the source code which you downloaded
with Mercurial.
The project comes with JavaScript tests which can be run from the command line as follows:
  $ java -jar JsTestDriver.jar --port 4224


This will start a server on http://localhost:4224
Open your favorite browser (we don't recommend IE as it is slow) at http://localhost:4224 and click the
“Capture This Browser” link.

From a new command line enter:
  $ java -jar JsTestDriver.jar --tests all


You should see something like this:
  $ java -jar JsTestDriver.jar --tests all
  ........
  Total 8 tests (Passed: 8; Fails: 0; Errors: 0) (10.00 ms)
    Chrome 4.0.202.0 MacIntel: Run 8 tests (Passed: 8; Fails: 0; Errors
  0) (10.00 ms)




OOPSLA 2009 Tutorial                               2                                     Miško Hevery
2 Understanding Manual Dependency Injection

2.1 Application Wiring Diagram
The application is a simple game of Tic-Tac-Toe as shown on the
right. The application is intentionally kept small for simplicity and
consists of these components.
   •   HTML (tic-tac-toe.html)
       ◦ #status: location of the status element.
       ◦ #board: location of the board game.
       ◦ #newGame: button reseting the game.
   •   Classes
       ◦ Board: Responsible for updating the DOM to show
         current state of the board.
       ◦ State: Internal representation of the board, which can
         determine a winner and reflects the DOM view of the
         board.
       ◦ Status: Updates the status DOM with the player turn
         and shows the winner.
       ◦ ManualDI: Bootstrap class which is responsible for wiring the application class together.
   •   Events
       ◦ Board Click: fired when the user clicks on the board game.
       ◦ New Game Click: fired when the user clicks on the “New Game Button.”

2.2 Understanding the Need for DI
A common misconception is that dynamic languages do not need Dependency Injection. The argument
is that in dynamic languages one can easily change the method definition at run time. Therefore no
method is untestable because all methods have seams where the normal flow of code execution can be
diverted for the purpose of test isolation through monkey-patching. While DI is very useful for testing,
it also aids the understanding of the code base. This benefit would be lost without DI. However, the
main fallacy of this argument stems from the fact that code is global constant, and monkey-patching the
code turns the global constants to mutable global state. While global constants don't present testability,
maintainability, or understandability problems, mutable global state presents problems in all previously
stated categories. It is true that while in static languages, such as Java, lack of DI spells death to
testability, lack of DI in dynamic languages still allows testability through monkey-patching. However,
it is not a best practice, and creates a problem of global state as stated above.




OOPSLA 2009 Tutorial                                 4                                     Miško Hevery
3 Designing Automated Dependency Injection Framework
The file src/ManualDI.js contains all of the wiring of the application. This is an error prone
process as it can lead to mistakes which are hard to detect because there are no automated tests.
The goal is to completely replace this file with automatic DI. To achieve automatic DI we need to solve
several issues as discussed in more detail next.

3.1 Replacing the Lost Information of Declared Types
The automatic Dependency Injection works by looking at a constructor of a class to determine the
parameter types, then recursively looking at those types. Dynamic Languages usually don't contain this
information and therefore we need a mechanism for declaring the missing information. To solve this
problem, we will declare a class constant containing the missing meta-data information for the
framework.
See scr/Board.js and notice the class constant inject
  Board = function (board, state) {
     this.board = board;
     this.nextPiece = "X";
     this.state = state;
  };

  Board.inject = {
     constructor:["#board", ":Status"],
     scope: "singleton",
     listen:[
       {source:"#newGame", event:”click”, call:'reset'},
       {source:"#board.box", event:”click”,
                              call:'addPiece', args:["@row", "@col"]}
     ]
  };


The inject meta-data contains information used to automatically construct the object graph. Notice
that the references are prefixed with special characters:
   •   “:” inject an object of a given type
   •   “#” inject a DOM element with a given id (this is a jQuery selector)
   •   “@” inject a property of a given object

3.2 Injecting Types
The most important ability for any Dependency Injection framework is to instantiate and inject
instances of a given type. We will use the following syntax to locate objects.
  var injector = new Injector();
  var board = injector.getInstance(“:State”);


The goal of this tutorial is to implement an Injector class which can read the meta-data information of
classes and use it to instantiate the objects.

OOPSLA 2009 Tutorial                                6                                     Miško Hevery
3.3 Injecting DOM Elements
In addition to injecting types, common in most static language DI frameworks, we would also like the
ability to inject DOM elements, a common need in JavaScript code, using the jQuery selector syntax.
Therefore, asking for #board is equivalent to jQuery(document).find(“#board”).

3.4 Injecting Events
DOM element listeners are also common in JavaScript, and therefore, the ability to inject listeners is
desired. Our meta-data contains the “listen” directive which specifies in declarative way the kind of
events the class should listen on.




OOPSLA 2009 Tutorial                                8                                      Miško Hevery
4 Implementing the Framework
The goal of the tutorial is to implement an automatic DI framework which can read the meta-data and
use it to instantiate our application. We want to change the current ManualDI.js class from:
  Main.prototype.bind = function() {
    var boardElement = this.doc.find("#board");
    var state = new State();
    var board = new Board(boardElement, state);
    var status = new Status(this.doc.find("#status"), state, function(){
        return board.nextPiece;
      });
    boardElement.find('.box').click(function(){
      var e = $(this);
      board.addPiece(e.attr('row'), e.attr('col'));
      status.update();
    });

       this.doc.find('#newGame').click(function(){
         board.reset();
         status.update();
       });
       board.reset();
  };


to an automatic wiring DI as shown below:

  Main.prototype.bind = function() {
     var injector = new Injector();
     var board = injector.getInstance(“:Board”);
     board.reset();
  };


The implementation will be done in the following way:
   •    All code will be test driven with unit tests and test first design

   •    First, we will implement the ability to instantiate pure objects such as State.js which will
        allow us to replace new State() with injector.getInstance(“:State”)

   •    Then, we will add the ability to inject jQuery selectors which will allow us to replace the
        complex instantiation of State with injector.getInstance(“:Status”)

   •    Finally, we will add the ability to inject listeners which will allow us to replace the whole
        manual DI with injector.getInstance(“:Board”) which is the root object.




OOPSLA 2009 Tutorial                                  10                                     Miško Hevery
5 Comparing the Resulting Wiring Code
When done with implementing the framework, we will discuss:
   •   The benefits of simplified code
   •   The different paths each of us took in the implementation
   •   The advantages and disadvantages of each of the implementations
   •   How we could further improve and grow the framework




OOPSLA 2009 Tutorial                              12                     Miško Hevery

More Related Content

What's hot

Java for Mainframers
Java for MainframersJava for Mainframers
Java for MainframersRich Helton
 
Automated Testing with Ruby
Automated Testing with RubyAutomated Testing with Ruby
Automated Testing with RubyKeith Pitty
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Fabien Potencier
 
Automated Testing With Watir
Automated Testing With WatirAutomated Testing With Watir
Automated Testing With WatirTimothy Fisher
 
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshellLennart Schoors
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for youSimon Willison
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax ApplicationsJulien Lecomte
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...Edureka!
 
Seam Introduction
Seam IntroductionSeam Introduction
Seam Introductionihamo
 
Mobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptMobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptfranksvalli
 
Web Projects: From Theory To Practice
Web Projects: From Theory To PracticeWeb Projects: From Theory To Practice
Web Projects: From Theory To PracticeSergey Bolshchikov
 
Test automation & Seleniun by oren rubin
Test automation & Seleniun by oren rubinTest automation & Seleniun by oren rubin
Test automation & Seleniun by oren rubinOren Rubin
 

What's hot (18)

Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
Automated Testing with Ruby
Automated Testing with RubyAutomated Testing with Ruby
Automated Testing with Ruby
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3
 
Automated Testing With Watir
Automated Testing With WatirAutomated Testing With Watir
Automated Testing With Watir
 
WordPress and Ajax
WordPress and AjaxWordPress and Ajax
WordPress and Ajax
 
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshell
 
How to make Ajax work for you
How to make Ajax work for youHow to make Ajax work for you
How to make Ajax work for you
 
High Performance Ajax Applications
High Performance Ajax ApplicationsHigh Performance Ajax Applications
High Performance Ajax Applications
 
Ajax Security
Ajax SecurityAjax Security
Ajax Security
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
 
Seam Introduction
Seam IntroductionSeam Introduction
Seam Introduction
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Selenium for-ops
Selenium for-opsSelenium for-ops
Selenium for-ops
 
Ant User Guide
Ant User GuideAnt User Guide
Ant User Guide
 
Mobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptMobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScript
 
Web Projects: From Theory To Practice
Web Projects: From Theory To PracticeWeb Projects: From Theory To Practice
Web Projects: From Theory To Practice
 
Css, xhtml, javascript
Css, xhtml, javascriptCss, xhtml, javascript
Css, xhtml, javascript
 
Test automation & Seleniun by oren rubin
Test automation & Seleniun by oren rubinTest automation & Seleniun by oren rubin
Test automation & Seleniun by oren rubin
 

Viewers also liked

Viewers also liked (7)

March2004-CPerlRun
March2004-CPerlRunMarch2004-CPerlRun
March2004-CPerlRun
 
resume_eric_tachibana
resume_eric_tachibanaresume_eric_tachibana
resume_eric_tachibana
 
09J1_ACG_Prospect
09J1_ACG_Prospect09J1_ACG_Prospect
09J1_ACG_Prospect
 
bestpractices
bestpracticesbestpractices
bestpractices
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
CombustionREFlexMorphing
CombustionREFlexMorphingCombustionREFlexMorphing
CombustionREFlexMorphing
 
Adweek and Neustar
Adweek and NeustarAdweek and Neustar
Adweek and Neustar
 

Similar to tut0000021-hevery

Make Mobile Apps Quickly
Make Mobile Apps QuicklyMake Mobile Apps Quickly
Make Mobile Apps QuicklyGil Irizarry
 
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014Gil Irizarry
 
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!Frédéric Harper
 
WebMatrix, see what the matrix can do for you!!
WebMatrix, see what the matrix can do for you!!WebMatrix, see what the matrix can do for you!!
WebMatrix, see what the matrix can do for you!!Frédéric Harper
 
State of jQuery June 2013 - Portland
State of jQuery June 2013 - PortlandState of jQuery June 2013 - Portland
State of jQuery June 2013 - Portlanddmethvin
 
Android course session 1 ( intoduction to java )
Android course session 1 ( intoduction to java )Android course session 1 ( intoduction to java )
Android course session 1 ( intoduction to java )Keroles M.Yakoub
 
WebObjects Developer Tools
WebObjects Developer ToolsWebObjects Developer Tools
WebObjects Developer ToolsWO Community
 
NCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile AppsNCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile AppsJohn M. Wargo
 
Opendaylight SDN Controller
Opendaylight SDN ControllerOpendaylight SDN Controller
Opendaylight SDN ControllerSumit Arora
 
Application Lifecycle Management
Application Lifecycle ManagementApplication Lifecycle Management
Application Lifecycle ManagementAmazon Web Services
 
J boss ide-tutorial
J boss ide-tutorialJ boss ide-tutorial
J boss ide-tutorialUTN
 
Django simplified : by weever mbakaya
Django simplified : by weever mbakayaDjango simplified : by weever mbakaya
Django simplified : by weever mbakayaMbakaya Kwatukha
 
O365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - MaterialO365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - MaterialThomas Daly
 
JavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionJavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionYoav Aharoni
 
Lecture 1 dev_environment
Lecture 1 dev_environmentLecture 1 dev_environment
Lecture 1 dev_environmentmoduledesign
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Robin O'Brien
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesFab L
 
Odo improving the developer experience on OpenShift - hack & sangria
Odo   improving the developer experience on OpenShift - hack & sangriaOdo   improving the developer experience on OpenShift - hack & sangria
Odo improving the developer experience on OpenShift - hack & sangriaJorge Morales
 
Webinar on How to use MyAppConverter
Webinar on How to use  MyAppConverterWebinar on How to use  MyAppConverter
Webinar on How to use MyAppConverterJaoued Ahmed
 
Docker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBMDocker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBMDocker, Inc.
 

Similar to tut0000021-hevery (20)

Make Mobile Apps Quickly
Make Mobile Apps QuicklyMake Mobile Apps Quickly
Make Mobile Apps Quickly
 
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
 
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
 
WebMatrix, see what the matrix can do for you!!
WebMatrix, see what the matrix can do for you!!WebMatrix, see what the matrix can do for you!!
WebMatrix, see what the matrix can do for you!!
 
State of jQuery June 2013 - Portland
State of jQuery June 2013 - PortlandState of jQuery June 2013 - Portland
State of jQuery June 2013 - Portland
 
Android course session 1 ( intoduction to java )
Android course session 1 ( intoduction to java )Android course session 1 ( intoduction to java )
Android course session 1 ( intoduction to java )
 
WebObjects Developer Tools
WebObjects Developer ToolsWebObjects Developer Tools
WebObjects Developer Tools
 
NCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile AppsNCDevCon 2017 - Cross Platform Mobile Apps
NCDevCon 2017 - Cross Platform Mobile Apps
 
Opendaylight SDN Controller
Opendaylight SDN ControllerOpendaylight SDN Controller
Opendaylight SDN Controller
 
Application Lifecycle Management
Application Lifecycle ManagementApplication Lifecycle Management
Application Lifecycle Management
 
J boss ide-tutorial
J boss ide-tutorialJ boss ide-tutorial
J boss ide-tutorial
 
Django simplified : by weever mbakaya
Django simplified : by weever mbakayaDjango simplified : by weever mbakaya
Django simplified : by weever mbakaya
 
O365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - MaterialO365 Developer Bootcamp NJ 2018 - Material
O365 Developer Bootcamp NJ 2018 - Material
 
JavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionJavaFX - Sketch Board to Production
JavaFX - Sketch Board to Production
 
Lecture 1 dev_environment
Lecture 1 dev_environmentLecture 1 dev_environment
Lecture 1 dev_environment
 
Introduction to Behavior Driven Development
Introduction to Behavior Driven Development Introduction to Behavior Driven Development
Introduction to Behavior Driven Development
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation Slides
 
Odo improving the developer experience on OpenShift - hack & sangria
Odo   improving the developer experience on OpenShift - hack & sangriaOdo   improving the developer experience on OpenShift - hack & sangria
Odo improving the developer experience on OpenShift - hack & sangria
 
Webinar on How to use MyAppConverter
Webinar on How to use  MyAppConverterWebinar on How to use  MyAppConverter
Webinar on How to use MyAppConverter
 
Docker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBMDocker for Devs - John Zaccone, IBM
Docker for Devs - John Zaccone, IBM
 

More from tutorialsruby

<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />tutorialsruby
 
TopStyle Help & <b>Tutorial</b>
TopStyle Help & <b>Tutorial</b>TopStyle Help & <b>Tutorial</b>
TopStyle Help & <b>Tutorial</b>tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>tutorialsruby
 
<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />tutorialsruby
 
<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
Winter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20JavascriptWinter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20Javascripttutorialsruby
 

More from tutorialsruby (20)

<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />
 
TopStyle Help & <b>Tutorial</b>
TopStyle Help & <b>Tutorial</b>TopStyle Help & <b>Tutorial</b>
TopStyle Help & <b>Tutorial</b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting <b>...</b>
 
<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />
 
<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
Winter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20JavascriptWinter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20Javascript
 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 

tut0000021-hevery

  • 1. Table of Contents Table of Contents........................................................................................................................................ i Instructors Biographies.............................................................................................................................. ii Agenda...................................................................................................................................................... iii Objectives Of the Course.......................................................................................................................... iv 1Setting Up Environment...........................................................................................................................6 1.1Setting up Mercurial.........................................................................................................................6 1.2Setting up Your Favorite Text Editor or Eclipse...............................................................................6 1.3Getting the Source............................................................................................................................ 6 1.4Setting up Test Runner..................................................................................................................... 8 2Understanding Manual Dependency Injection.......................................................................................10 2.1Application wiring diagram............................................................................................................10 2.2Understanding the Need for DI...................................................................................................... 10 3Designing Automated Dependency Injection Framework..................................................................... 12 3.1Replacing the Lost Information Of Declared Types.......................................................................12 3.2Ability to Inject Types.................................................................................................................... 12 3.3Ability to Inject DOM Elements.................................................................................................... 14 3.4Ability to Inject Events...................................................................................................................14 4Implementing the Framework................................................................................................................ 16 5Comparing the Resulting Wiring Code.................................................................................................. 18 OOPSLA 2009 Tutorial i Miško Hevery
  • 2. Instructors Biographies Miško Hevery misko@hevery.com Google, Inc. … works as an Agile Coach at Google where he is responsible for coaching Googlers to maintain the high level of automated testing culture. This allows Google to do frequent releases of its web applications with consistent high quality. Previously he worked at Adobe, Sun Microsystems, Intel, and Xerox (to name a few), where he became an expert in building web applications in web related technologies such as Java, JavaScript, Flex and ActionScript. He is very involved in Open Source community and an author of several open source projects. Recently his interest in Test-Driven- Development turned into Testability Explorer (http://code.google.com/p/testability-explorer) and JsTestDriver (http://code.google.com/p/js-test-driver) with which he hopes to change the testing culture of the open source community. OOPSLA 2009 Tutorial ii Miško Hevery
  • 3. Agenda 1. Setting up your environment 1. Setting up Mercurial 2. Setting up your favorite text editor or Eclipse 3. Setting up JsTestRunner 2. Getting the source 1. Checking out the source code 2. Running the code 3. Understanding manual Dependency Injection 1. Application wiring diagram 2. Understanding the need for DI 4. Designing automated Dependency Injection framework 1. Replacing the lost information of declared types 2. Injecting types 3. Injecting DOM elements 4. Injecting events 5. Implementing the framework 6. Cleaning up the bootstrap wiring code 7. Discussion of before and after code OOPSLA 2009 Tutorial iii Miško Hevery
  • 4. Objectives of the Course Dependency Injection is a good practice even in dynamic languages. Because many dynamic languages do not have type declarations, building an automatic DI framework presents its own set of challenges. Furthermore, many concepts, such as scopes that are often associated with threads, change their meaning in most dynamic languages which are single threaded. In this tutorial, we will build a simple automatic DI framework and solve many of the challenges which are associated with lack of declared types in such languages. • Understand the need for Dependency Injection ◦ Dynamic languages are not special and still need Dependency Injection • How to replace the lack of typing information in dynamic languages • Injection is not limited to injecting object instances ◦ Building our own domain specific language ◦ The obvious: Injecting types ◦ Thinking outside the box: Injecting DOM elements, and events • Dependency Injection simplifies the wiring of the application logic ◦ Comparing application bootstrap process before and after OOPSLA 2009 Tutorial iv Miško Hevery
  • 5. 1 Setting Up Environment Before the tutorial we recommend that you complete all of the following steps: 1.1 Setting up Mercurial The code for this project is located at: • http://bitbucket.org/misko/misko-hevery-oopsla-09 You will need to install Mercurial for your platform: • Home: http://mercurial.selenic.com/wiki/ • Download: http://mercurial.selenic.com/wiki/Download? action=show&redirect=BinaryPackages 1.2 Getting the Source After installing a copy of Mercurial you can download the source using the following command: $ hg clone https://misko@bitbucket.org/misko/misko-hevery-oopsla-09/ 1.3 Setting up Your Favorite Text Editor or Eclipse You will need you favorite Text Editor for editing JavaScript, HTML, and CSS. We recommend Eclipse which you can download and install from the link bellow. We have already set up an Eclipse workspace for you in the Mercurial repository. • Download Eclipse: http://www.eclipse.org/downloads/ • Install JavaScript Editor: From Eclipse ◦ Help → Install New Software ◦ Select: Work With: Galileo ◦ Select: Web, XML, and Java EE Development ◦ Complete the installation by clicking the “Next” button • Install JsTestDriver for Eclipse ◦ Help → Install New Software ◦ Select: Work With: and enter: http://js-test-driver.googlecode.com/svn/update/ ◦ Select JsTestDriver ◦ Complete the installation by clicking the “Next” button • Import the DI project which you downloaded with Mercurial. OOPSLA 2009 Tutorial Miško Hevery
  • 6. 1.4 Setting up Test Runner NOTE: All commands need to be executed from the root of the source code which you downloaded with Mercurial. The project comes with JavaScript tests which can be run from the command line as follows: $ java -jar JsTestDriver.jar --port 4224 This will start a server on http://localhost:4224 Open your favorite browser (we don't recommend IE as it is slow) at http://localhost:4224 and click the “Capture This Browser” link. From a new command line enter: $ java -jar JsTestDriver.jar --tests all You should see something like this: $ java -jar JsTestDriver.jar --tests all ........ Total 8 tests (Passed: 8; Fails: 0; Errors: 0) (10.00 ms) Chrome 4.0.202.0 MacIntel: Run 8 tests (Passed: 8; Fails: 0; Errors 0) (10.00 ms) OOPSLA 2009 Tutorial 2 Miško Hevery
  • 7. 2 Understanding Manual Dependency Injection 2.1 Application Wiring Diagram The application is a simple game of Tic-Tac-Toe as shown on the right. The application is intentionally kept small for simplicity and consists of these components. • HTML (tic-tac-toe.html) ◦ #status: location of the status element. ◦ #board: location of the board game. ◦ #newGame: button reseting the game. • Classes ◦ Board: Responsible for updating the DOM to show current state of the board. ◦ State: Internal representation of the board, which can determine a winner and reflects the DOM view of the board. ◦ Status: Updates the status DOM with the player turn and shows the winner. ◦ ManualDI: Bootstrap class which is responsible for wiring the application class together. • Events ◦ Board Click: fired when the user clicks on the board game. ◦ New Game Click: fired when the user clicks on the “New Game Button.” 2.2 Understanding the Need for DI A common misconception is that dynamic languages do not need Dependency Injection. The argument is that in dynamic languages one can easily change the method definition at run time. Therefore no method is untestable because all methods have seams where the normal flow of code execution can be diverted for the purpose of test isolation through monkey-patching. While DI is very useful for testing, it also aids the understanding of the code base. This benefit would be lost without DI. However, the main fallacy of this argument stems from the fact that code is global constant, and monkey-patching the code turns the global constants to mutable global state. While global constants don't present testability, maintainability, or understandability problems, mutable global state presents problems in all previously stated categories. It is true that while in static languages, such as Java, lack of DI spells death to testability, lack of DI in dynamic languages still allows testability through monkey-patching. However, it is not a best practice, and creates a problem of global state as stated above. OOPSLA 2009 Tutorial 4 Miško Hevery
  • 8. 3 Designing Automated Dependency Injection Framework The file src/ManualDI.js contains all of the wiring of the application. This is an error prone process as it can lead to mistakes which are hard to detect because there are no automated tests. The goal is to completely replace this file with automatic DI. To achieve automatic DI we need to solve several issues as discussed in more detail next. 3.1 Replacing the Lost Information of Declared Types The automatic Dependency Injection works by looking at a constructor of a class to determine the parameter types, then recursively looking at those types. Dynamic Languages usually don't contain this information and therefore we need a mechanism for declaring the missing information. To solve this problem, we will declare a class constant containing the missing meta-data information for the framework. See scr/Board.js and notice the class constant inject Board = function (board, state) { this.board = board; this.nextPiece = "X"; this.state = state; }; Board.inject = { constructor:["#board", ":Status"], scope: "singleton", listen:[ {source:"#newGame", event:”click”, call:'reset'}, {source:"#board.box", event:”click”, call:'addPiece', args:["@row", "@col"]} ] }; The inject meta-data contains information used to automatically construct the object graph. Notice that the references are prefixed with special characters: • “:” inject an object of a given type • “#” inject a DOM element with a given id (this is a jQuery selector) • “@” inject a property of a given object 3.2 Injecting Types The most important ability for any Dependency Injection framework is to instantiate and inject instances of a given type. We will use the following syntax to locate objects. var injector = new Injector(); var board = injector.getInstance(“:State”); The goal of this tutorial is to implement an Injector class which can read the meta-data information of classes and use it to instantiate the objects. OOPSLA 2009 Tutorial 6 Miško Hevery
  • 9. 3.3 Injecting DOM Elements In addition to injecting types, common in most static language DI frameworks, we would also like the ability to inject DOM elements, a common need in JavaScript code, using the jQuery selector syntax. Therefore, asking for #board is equivalent to jQuery(document).find(“#board”). 3.4 Injecting Events DOM element listeners are also common in JavaScript, and therefore, the ability to inject listeners is desired. Our meta-data contains the “listen” directive which specifies in declarative way the kind of events the class should listen on. OOPSLA 2009 Tutorial 8 Miško Hevery
  • 10. 4 Implementing the Framework The goal of the tutorial is to implement an automatic DI framework which can read the meta-data and use it to instantiate our application. We want to change the current ManualDI.js class from: Main.prototype.bind = function() { var boardElement = this.doc.find("#board"); var state = new State(); var board = new Board(boardElement, state); var status = new Status(this.doc.find("#status"), state, function(){ return board.nextPiece; }); boardElement.find('.box').click(function(){ var e = $(this); board.addPiece(e.attr('row'), e.attr('col')); status.update(); }); this.doc.find('#newGame').click(function(){ board.reset(); status.update(); }); board.reset(); }; to an automatic wiring DI as shown below: Main.prototype.bind = function() { var injector = new Injector(); var board = injector.getInstance(“:Board”); board.reset(); }; The implementation will be done in the following way: • All code will be test driven with unit tests and test first design • First, we will implement the ability to instantiate pure objects such as State.js which will allow us to replace new State() with injector.getInstance(“:State”) • Then, we will add the ability to inject jQuery selectors which will allow us to replace the complex instantiation of State with injector.getInstance(“:Status”) • Finally, we will add the ability to inject listeners which will allow us to replace the whole manual DI with injector.getInstance(“:Board”) which is the root object. OOPSLA 2009 Tutorial 10 Miško Hevery
  • 11. 5 Comparing the Resulting Wiring Code When done with implementing the framework, we will discuss: • The benefits of simplified code • The different paths each of us took in the implementation • The advantages and disadvantages of each of the implementations • How we could further improve and grow the framework OOPSLA 2009 Tutorial 12 Miško Hevery