saskodh / framework

Lightweight web framework for NodeJS inspired by Spring framework
26 stars 5 forks source link

Implement @Qualifier decorator #16

Closed damjangelovski closed 8 years ago

damjangelovski commented 8 years ago

Background

When we want to use TypeScript interfaces we cannot use them as injection tokens, because they are removed after the compilation. As a workaround for this the user needs to create a token (Symbol) for each interface he defines, to be later used for injection. Because classes can implement multiple interfaces, we need a way to specify multiple tokens.

Goal

Make an implementation of @Qualifier decorator that can be used together with the @Component decorator for specifying the alias injection tokens. Example:

const IWalkableToken = Symbol('IWalkableToken');
interface IWalkable { ... }

const ITalkableToken = Symbol('ITalkableToken');
interface ITalkable { ... }

@Qualifier(ITalkableToken)
@Qualifier(IWalkableToken)
@Component()
class Component implements IWalkable, ITalkable  {  ...  }

@Component()
class ComponentHolder {

    @Inject()
    private component: Component;

    @Inject(ITalkableToken)
    private talkable: ITalkable;

    @Inject(IWalkableToken)
    private walkable: IWalkable;
}