container-ioc icon indicating copy to clipboard operation
container-ioc copied to clipboard

Simplify dependency injection syntax in constructor with Reflect API

Open basedalexander opened this issue 7 years ago • 6 comments

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

  • Use Reflect API to simplify injection syntax.
  • That can only be possible with Typescript, don't try to apply this feature to es5+.
  • Update docs according to this feature.

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

@Injectable()
class HttpService {
    
}

container.register([HttpService, AppService])

basedalexander avatar Nov 19 '17 11:11 basedalexander

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.

Tolowe avatar Nov 19 '17 13:11 Tolowe

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

basedalexander avatar Dec 02 '17 10:12 basedalexander

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

Tolowe avatar Dec 02 '17 18:12 Tolowe

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

basedalexander avatar Dec 02 '17 19:12 basedalexander

@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.

DonJayamanne avatar Jan 04 '18 00:01 DonJayamanne

@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.

basedalexander avatar Jan 04 '18 21:01 basedalexander