typesoft / container-ioc

Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
https://www.npmjs.com/package/container-ioc
MIT License
102 stars 25 forks source link

Simplify dependency injection syntax in constructor with Reflect API #102

Open basedalexander opened 6 years ago

basedalexander commented 6 years ago

Implementing this feature is a great way to learn Reflect API and Typescript decorators. I've provided link to a great article on this topic.

Typescript Decorators and Reflect API


@Injectable()
class AppService {
    constructor(private http: HttpService) {} // instead of @Inject(HttpService) private http: HttpService
}

@Injectable()
class HttpService {

}

container.register([HttpService, AppService])
Tolowe commented 6 years ago

Thanks for suggestion @thohoh I'll take a look at this one. Looks like an interesting topic to learn. I'll reach out if I have any questions.

basedalexander commented 6 years ago

@Tolowe Please let me know if you're currently working on it or not. If not, I'll implement that on this weekend.

Tolowe commented 6 years ago

@thohoh I didnt make it very far on this one - go for it. Sorry about that.

basedalexander commented 6 years ago

@Tolowe That's okay, we all have things to do apart from open source :D. I'm gonna roll it out soon.

DonJayamanne commented 6 years ago

@thohoh I'd suggestion you re-consider the use reflect-metadata due to a known limitation (further on this below). Currently we're using inversify in one of our projects and have stumbled across a serious issue that has lead us to re-evaluate the use of inversify and look for other IOC frameworks in Typescript (which has lead us to container-ioc).

Here's a link to the original issue: https://github.com/inversify/InversifyJS/issues/737

Or you could possibly implement this feature, as a feature that would work if reflect-metadata is available (but not something that I'd add as a dependency into the container-ioc). I.e. if users want the proposed capabilities suggested by this issue, then they'd need to manually import reflect-metadata.

basedalexander commented 6 years ago

@DonJayamanne Hello Don. Yes I totally agree with your suggestion. I had this in my mind at the beginning. It's really easy to implement.