DiCoI - DI/IoC Container
DiCoI is a simple Dependency injection / Inversion of Control Container example in Typescript that enables developers handle automatic dependency injection.
Installation
Using npm:
$ npm install dicoi
Using yarn:
$ yarn add dicoi
Example
Lets suppose a RatesService
that connects to an API to get currency rates and logs results. It depends on ApiService and Logger. ApiService
also depends on Logger
and also requires a param. To automatically initialize RatesService with those dependencies, we'd like to be able to do:
; ; .inject .and .and .intoRatesService; rates.getRate'USD', .thenconsole.log;
DiCoI gives us a Container
that will handle those dependenies for us. It has declarative methods that enable us to inject(DEPENDENCY_PARAM_OBJET).and(DEPENDENCY_CLASS_OBJET).into(MAIN_CLASS)
For this to work, you'll need to decorate RatesService
and ApiService
with @Injectable
decorator, and any dependency (non-class) param must be decorated with @Inject
decorator.
; ;
NOTE: that
Logger
class does not need any decorator, because we do not need to inject anything into it.
You can view this working example in this demo