microsoft / tsyringe

Lightweight dependency injection container for JavaScript/TypeScript
MIT License
5.13k stars 168 forks source link

Auto register container to itself for dyanmic inject #164

Open Austaras opened 3 years ago

Austaras commented 3 years ago

In angular we could

private injector: Injector

so that we can get depenency dynamically at any time instead of when construted, this is specifically useful for inject multi instance like http interceptor. I guess it's already possible for tsyringe to do so, only thing needed is a standard token and some modify to container factory

MeltingMosaic commented 3 years ago

You can already do this, I guess, by assigning the container to a private member as you have suggested. But perhaps I'm not fully understanding the full context here - could you provide a fuller example of how you would like this to work?

Austaras commented 3 years ago

Yes I could assign it to a member, however for complex requirment like hierarchy inject I'd like to get child container instead of root container. Of course I could pass it manually -- but I think it's better to automate this process

@inject
class A {
   constructor(private container: Container) {}

   run() {
     this.container.resolveAll()
   }
}