tsjs-di
A seamless dependency injector for typescript and javascript.
Installation
npm install tsjs-di
Usage
Typescript
Importing the dependency injector
;
Creating providers
./providerA.ts
Providers will be injected as singletons into all dependants,
to inject multi instance a provider must be decorated with @Provider({multi: true})
or registered with the multi option set to true.
A Provider can also have its dependencies injected if it is decorated with @Provider()
or Inject()
.
./providerB.ts
;;
Creating dependants
A dependant class must be marked with @Inject()
decorator to have its dependencies injected.
./dependantA.ts
;;
A dependency can also be injected into a class property by decorating the property with @Inject()
decorator and specifying the type of the dependency.
./dependantB.ts
;;
Register providers and running the DI
index.ts
;;;;; // register by specifying the class onlyDI.registerProviderA // Or by supplying a provider objectDI.register; // specifying a factoryDI.register; // Or by specifiying an array with the above methodsDI.register; // You can also provide a value; DI.register; // putting the DI into action ; // this will produce an error in the IDE// altentively; app.emit;
The DI
will inject the dependencies as long as the parameters are not defined on call.
Javascript
Since there is no type support in javascript, dependencies have to be injected manually. This methods can also be used in typescript.
Simple Example
const DI = DI; name = 'Provider'; { } { thisprovider = provider; }// register the providerDI; const dep = ;// orconst dep = DI;
Factory Example
./logger.js
{ } { console; } moduleexports = Logger;
./factory-provider.js
const DI = DI; { thislogger = logger; } { thislogger; } static { return di; } moduleexports = FactoryProvider;
./index.js
const DI = DI;const Logger = ;const FactoryProvider = ; DIclear;DI; const provider = DI;provide;
API
.register(providers)
Adds dependency providers to the DI, it takes a class, a provider object or an array of classes and provider object.
provider object
The provider object must have:
provide
: A string or a Class of the provider
and any of the following:
useClass
: The class to be used to satisfy the dependency, can be different from the one provided.useFactory
: A function to be used to create or instantiate the provider.useValue
: The value to be used to satisfy the dependency, it can be of any type.multi
: A boolean that indicates if the provider should be multi instance or not.
Throws a TypeError
if called with an invalid provider
.inject(type)
Returns a registered provider that matches the specified type. Returns the same instance unless the dependency was registered with the multi option.
type
can be a string or Class from which the provider can be identified.
Throws a ReferenceError
if the provider is not registered.
.get(type)
Returns a new instance of a registered provider that matches the specified type.
type
can be a string or Class from which the provider can be identified.
Throws a ReferenceError
if the provider is not registered.
.exist(type)
Returns true if the specified provider exists, else returns false.
type
can be a string or Class from which the provider can be identified.
.clear()
Removes all providers