0.1.2 • Published 7 years ago

@t2ee/c3po v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

#Introduction

This is a dependency injection library for typescript.

#Example

@ioc.Provides
class RandomInjectable {
    num: number = Math.random();
}
@ioc.AutoWired
class RandomInjectableTest {
    @ioc.Inject
    inject: RandomInjectable;
}
new RandomInjectableTest().inject.number // some random number 

Flow

instanciate

Inject -> AutoWired -> constructor

provide

Scope -> Provider -> Container -> result

#API

@Inject: PropertyDecorator | ParameterDecorator

It can be used on properties and constructor parameters. use @AutoWired on the class to trigger injection

@AutoWired: ClassDecorator

Mount injections

@Provides: ClassDecorator

Auto provide the class

@Provide(Provider): ClassDecorator

@Provide(targetClass, Provider): ClassDecorator

@Provide(targetClass): ClassDecorator

Provide with different provider, and/or provide for different class

@Scoped(Scope): ClassDecorator

Apply scope on target class

@Singleton: ClassDecorator

Globally singleton

@ClassSingleton: ClassDecorator

Class-wide singleton. One instance per class

interface Provider

interface Provider {
    get(target?);
}

Scope

abstract class Scope {
    abstract resolve<T>(provider: Provider<T>, target?): T;
}

Container

class Container {
    static provider<T>(targetClass, Provider<T>);
    static get<T>(providedClass, target?);
}