Skip to content

rodnaph/Binder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Binder

Binder is a simple module for binding abilities together between Javascript objects. It takes the nouns out of dependency injection and ties functions together between objects in a lightweight, decoupled manner.

Contrived Example

Objects specify their abilities using the can property array, and they specify abilities they require using the needs property array.

Car.prototype = {
  can: [ 'driveToWork' ],
  needs: [ 'goForwards', 'turnLeft', 'turnRight' ],
  // ...
};

Here, the car advertises it has the ability to drive you to work, but that it needs to collaborate with objects that can help it go forwards, turn left, and turn right. This kind of object would then want to collaborate with something like an engine...

Engine.prototype = {
  can: [ 'goForwards', 'goBackwards' ],
  // ...
};

Usage Example

To use Binder you can install it through npm.

var binder = require( 'binder' ).create();

It then takes functions to construct objects as the single argument to its make method.

binder.make( Engine );
binder.make( SteeringWheel );
var car = binder.make( Car );

The car will then be able to use the abilities that were provided by the engine and the steering wheel.

Car.prototype = {
  // ...
  driveToWork: function() {
    this.goForwards();
    this.turnLeft();
    // etc...
  }
};

The car is then communicating with its collaborators without being dependent on who they are, the only coupling is by the specific abilities that it requires.

Binder Abilities

The binder object provides the ability binderMake which can make other objects that possess/require abilities.

Ability Namespace

The astute reader chortle will notice that ability names form a single namespace. Abilities provided and consumed by objects share a single space within a Binder object. So how do you handle collisions? Well you can provide mapping each side to map abilities to/from instance methods of your choosing. So to provide an ability implemented by a named method do:

Car.prototype = {
  can: [
    'driveToWork:makeTheCarDriveToWork'
  ],
  makeTheCarDriveToWork: function() {
    // ability impl
  }
};

And to add an ability to your object use the same syntax:

Van.prototype = {
  needs: [
    'goFowards:makeTheCarGoForwards'
  ],
  driveToWork: function() 
    this.makeTheCarGoForwards();
  }
};

So the ability name namespace, no longer bound to method names, can be application defined. (eg. 'com.mysite.somePackage.abilityName')

Unit Testing Abilities

There's also a TestBinder object available via the createTest() method, that can be used in unit testing. The main method it provides in addition to the normal binder methods is mock. This allows you to mock an ability for use in unit testing.

binderTest = require( 'binder' ).createTest();
binderTest.mock( 'someAbility', function() { ... } );
var obj = binderTest.make( MyClass );

Credits

This is just a toy concept at the moment, and was inspired by ideas from: http://thorstenlorenz.wordpress.com/2011/07/23/dependency-injection-is-dead-long-live-verbs/

About

Object ability binder for JS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published