thiagobustamante / typescript-ioc

A Lightweight annotation-based dependency injection container for typescript.
MIT License
526 stars 64 forks source link

2 Files for abstract class (interface) and implementation result in error #23

Closed nolazybits closed 6 years ago

nolazybits commented 6 years ago

Hi there,

So I used to add abstract class and implemetation in the same file and everything was sweet.. Now if I try to separate the definition to the implementation it fails

eg

afile.ts

export abstract class ITest
{
    public abstract aProperty: string;
}

@Singleton
@Provides(ITest)
export class Test implements ITest
{
    public aProperty: string;
}

will work fine whereas ITest.ts

export abstract class ITest
{
    public abstract aProperty: string;
}

Test.ts

@Singleton
@Provides(ITest)
export class Test implements ITest
{
    public aProperty: string;
}

will still work but the object is going to be an empty object {}

My guess is that nowhere in my code do I have a link to the Test.ts file, hence not loaded

...
@Inject
protected test: ITest;
...

What I want to achieve -> trying to have a really modular code base, and providing for instance an definition for a Logger but many implementation in a Library. Then the user could choose what implementation to use (ConsoleLogger, CloudwatchLogger, MultiLogger, ...). I guess then to be able to achieve this I need to use the Container.bing(ITest).to(Test) and won't be able to use decorator.

Is that correct or is there a solution to my problem just using decorators?

nolazybits commented 6 years ago

Anyone can confirm or affirm what I have written please?

thiagobustamante commented 6 years ago

Hi @nolazybits . Sorry for the late response.

The problem is that typescript-ioc does not scan any folder looking for classes. Your classes must be previously imported. I updated the documentation to explain better this point and to show how you can tell the Container where to find your classes.

Please, take a look at this

nolazybits commented 6 years ago

Thank you much for the extra doc. I have used the bind method but will look into the addSource. Thanks again :+1: