Curli-DI
A Dependency Injection (DI) library in typescript without using decorators.
Installation
Install by npm
npm install --save curli-di
Basic Usage
;; ; //register the dependencies for the classcontainer.registerService"dataBase", , Database;container.registerService"foo", , Foo; //creating the service;
Registering external dependencies/properties:
Some time we need to inject properties or other kind of values into our services, for this we use the class ExternalDependencies like here:
;; // Register previous dependencies already instantiated;externalDependencies.add"dataBaseUser", "root";externalDependencies.add"dataBasePass", ""; ; //register the dependencies for the classcontainer.registerService"dataBase", , Database;container.registerService"foo", , Foo; //creating the service;
As this example shows, to access to any external dependency or property we need to add the prefix @.
Also to inject or use a external dependency we can use the registerServiceBuilded method in the container after we initialized it:
; ;container.registerServiceBuilded"dataBaseUser", "root"; ;
Registering external dependencies/properties with an object in bulk mode:
;;;; // Register previous dependencies already instantiated;externalDependencies.bulk; ; //register the dependencies for the classcontainer.registerService"dataBase", , Database;container.registerService"foo", , Foo;container.registerService"lang", , Lang;container.registerService"oauth", , Oauth; if container.get"@useOauth" //creating the service;
Circular dependency:
To create a circular dependency between two services, one of the then will receive their dependencies with a different method than the constructor.
; ; //registering of dependencies with the classescontainer.registerService"foo", , Foo;container.registerService"taa", , Taa, false, Taa.prototype.injectDependencies; //creating the service;
Registering services descriptions in different sides of the application:
If we want to split the responsibility of register services in different sides of the application we can perform it using the ExternalServicesRegister class:
;; // Register previous dependencies already instantiated externalServicesRegister = new ExternalServicesRegister; //register the dependencies for the classexternalServicesRegister.registerService"dataBase", , Database;externalServicesRegister.registerService"foo", , Foo; container.registerExternalServiceRegisterexternalServicesRegister; //creating the service;
Instantiate one or more services in a specific moment (like when we start the app) using autoInit option:
;;;;; ; //register the dependencies for the classcontainer.registerService"dataBase", , Database, true;container.registerService"foo", , Foo;container.registerService"lang", , Lang;container.registerService"oauth", , Oauth, true; //start dataBase service and oauthcontainer.callAllServicesWithAutoInit;