typeioc

Dependency injection container for node typescript

TypeIOC

...XWindows

Dependency injection container for node typescript.

###Install

Install typescript globally

npm install typescript -g

Install typeioc

npm install typeioc

###Usage

Load typeioc:

var typeioc = require('typeioc');

Assuming TestBase class and Test class exist somewhere

Basic resolution (JS):

var containerBuilder = typeioc.createBuilder();
containerBuilder.register(TestBase).as(function() {return  new Test() });
var container = containerBuilder.build();
var actual = container.resolve(TestBase);

With type checking (TS):

Copy typeioc.d.ts definition file from d.ts folder to your project and reference it within ts files.

/// <reference path="typeioc.d.ts" />
import typeioc = require("typeioc");
 
var containerBuilder = typeioc.createBuilder();
containerBuilder.register<TestBase>(TestBase)
    .as(() => new Test());
var container = containerBuilder.build();
var actual = container.resolve<TestBase>(TestBase);

Registering with dependencies:

containerBuilder.register<Test2Base>(Test2Base)
    .as(() => new Test2());
containerBuilder.register<Test1Base>(Test1Base)
    .as((c) => {
        var test2 = c.resolve(Test2Base);
        return new Test3(test2);
    });

Fluent API:

containerBuilder.register<Test1Base>(Test1Base)           // register component Test1Base
    .as(() => new Test5())                                // as instance of Test5
    .initializeBy((c, item) => item.coolMethodHere())     // invoke initialization on resolved instances
    .dispose((item : testData.Test5)  => item.Dispose())  // invoke disposal when disposing container
    .named('Some Name')                                   // resolve with specific name
    .within(typeioc.Types.Scope.Hierarchy)                // specifies instance reusability
    .ownedBy(typeioc.Types.Owner.Container);              // specifies instance ownership
 
 
var container = containerBuilder.build();                 // create an instance of container
 
container
    .resolveWith<TestData.Test1Base>(Test1Base)           // resolve an instance of Test1Base
    .args(arg1, arg2)                                     // with arguments
    .attempt()                                            // try resolve (do not throw if not found)
    .name(someName)                                       // with name (for named registrations)
    .dependencies([d1, d2])                               // with dependencies (for things Test1Base depends on)
    .cache()                                              // with cached resolution value => container.cache.Test1Base
    .exec();                                              // resolve
 

With Add-ons:

Copy typeioc.addons.d.ts definition file from d.ts folder to your project and reference it within ts files.

Interceptors

/// <reference path="typeioc.d.ts" />
/// <reference path="typeioc.addons.d.ts" />
 
var typeioc = require('typeioc');
var addons = require('typeioc/addons');
 
 
var containerBuilder = typeioc.createBuilder();
 
containerBuilder.register(Math)
    .as(c => {
 
    var interceptor = addons.Interceptors.create();
 
    return interceptor.intercept(Math, [{
            method: 'pow',
            type: addons.Interceptors.CallInfoType.Method,
            wrapper: function (callInfo:Addons.Interceptors.ICallInfo) {
 
                console.log('Before execute : ' + callInfo.args[0] + ' ' + callInfo.args[1]);
 
                var result = callInfo.invoke(callInfo.args);
 
                console.log('After execute : ' + result);
 
                return callInfo.args[0] + callInfo.args[1];
            }
        }]);
    });
 
var container = containerBuilder.build();
var actual = container.resolve<Math>(Math);
actual.pow(2,3); // 5
actual.log(1);   // still 0
 

###Features

  • [x] - Type compliance checking.
  • [x] - Late instances creation through lambda expressions.
  • [x] - Dependencies resolution.
  • [x] - Named instances resolution.
  • [x] - Custom instance initialization.
  • [x] - Custom instance disposal.
  • [x] - Instance scoping.
  • [x] - Instance ownership.
  • [x] - Module registration
  • [x] - Fluent API.
  • [x] - Configuration (JS).
  • [x] - Runtime / Dynamic dependencies substitution.
  • [x] - Cached resolution results.
  • [x] - Interceptors.
  • [ ] - ES7 decorators style resolution.
  • [ ] - ES6 codebase + weak references.
  • [ ] - Instance lifetime scoping APIs.
  • [ ] - Promises configuration.
  • [ ] - In-browser usage support.
  • [ ] - Usage with 3d part libraries.
  • [ ] - Full API documentation.

###Running tests

npm test