nicojs / typed-inject

Type safe dependency injection for TypeScript
Apache License 2.0
456 stars 23 forks source link

Question: How to get an instance of a class only from its token? #56

Open rmtuckerphx opened 2 years ago

rmtuckerphx commented 2 years ago

I have code that runs in a pipeline and I want to use injection to provide an instance of 1 class for local dev, another for unit tests and the "real" one for production.

  1. Create the injector: jovo.$injector = createInjector();
  2. In code that only runs during dev, provide the class jovo.$injector.provideClass('myService', ServiceDev);
  3. ServiceDev class implements ServiceInterface. Other classes ServiceTest, ServiceProd.
  4. Get an instance of the class and assign to interface: const myService = jovo.$injector.injectClass('myService')
  5. Currently ServiceDev has no ctor params to inject. Other classes will.