0.2.3 • Published 10 years ago

dekorator v0.2.3

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

Dekorator

wercker status

Simple dependency injection lib exposing inject and singleton decorators. Dekorator can be used with Node or in browser with an ES6 transpiler such as (Babel)https://babeljs.io/.

Dekorator is an extremely stripped down dependency injection library. It is meant to provide a lightweight solution for embedding into other libraries. If you are looking for a robust solution with lots of features this isn't it. It has a very specific use case. This doesn't mean it won't work exactly as indicated but rather you may need additional features it does not provide.

Install

For use with Node

$ npm install dekorator

For use in browser & JSPM

NOTE: jspm is a package manager which sets up your project for use with System.js, the ES6 module loader system. You can read more on (jspm here)http://jspm.io.

jspm install npm:dekorator

or

jspm install github:origin1tech/dekorator

Getting Started

Using dekorator is very simple. You need merely decorate an ES6 class to have it's dependency(s) injected into the constructor or specify the class as a singleton.

Injecting

// NOTE: import below shown using System.js if you
// reference typings (dist/index.d.ts) you can import
// as ... from 'dekorator';
import {inject, invoke} from 'origin1tech/dekorator';

class Vehicle {
	constructor() {
	}
}

@inject(Vehicle)
class Car {
	constructor(vehicle) {
		this.vehicle = vehicle;
	}
	getVehicle() {
		return this.vehicle;
	}
}

// Invoke the class.
let car = invoke(Car);

// do something with car and
// injected vehicle.
let vehicle = car.getVehicle();

singleton

Nothing special here simply decorate your class with "@singleton" and it just works. Decorating with this decorator will ensure the class is a singleton in turn it will return the same instance when injected in other classes.

@singleton
class MyClass {
	constructor() {}
}

Additional Notes

One thing that trips up devs when first using decorators, well at least for those that actually use semi colons is that when using an ES7/ES2016 decorator you DO NOT use a semi colon after the decoration. Think of it in terms of a fluent api where you wouldn't terminate as you're using dot notation to further define some class/function etc.

What NOT to do

The below example with fail and not work

@inject(OtherClass);
class MyClass {
	constructor() {}
}

Testing

You will notice a few specs in the test folder which can be run using Mocha. Simply type mocha in your terminal from the root of the project or run:

npm test

License

Basically you can do just about anything you like. Enjoy!

See LICENSE.md