- Notifications
You must be signed in to change notification settings - Fork25
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
License
typesoft/container-ioc
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
is aDependency Injection /Inversion of Control (IoC) container package for Javascript and Node.js applications powered by Typescript . It manages the dependencies between classes, so that applications stay easy to change and maintain as they grow.
- Well-known Angular DI API.
- No external dependencies.
- Life Time control.
- Hierarchical containers.
- Resolves values using Classes,Factories andValues.
- Descriptive error messages.
- 97% test coverage.
npm install --save container-ioc
Code examples below are written in Typescript. Checkexamples/javascript for examples written in Javascript.
Possible values for types:Symbol,string,Object.
interfaceIApplication{run():void;}interfaceIService{serve():void;}constTApplication=Symbol('IApplication');constTService=Symbol('IService');
import{Injectable,Inject}from'container-ioc';@Injectable()exportclassApplicationimplementsIApplication{constructor(@Inject(TService)privateservice:IService){}run():void{this.service.serve();}}@Injectable()exportclassServiceimplementsIService{serve():void{// serves}}
import{Container}from'container-ioc';letcontainer=newContainer();container.register([{token:TApplication,useClass:Application},{token:TService,useClass:Service}]);
letapp=container.resolve(TApplication);app.run();
Since Javascript does not support parameter decorators, use alternative API for declaring dependencies. In this case we don't useInject decorator. Seeexamples/javascript for more.
@Injectable([TService])classService{constructor(service){this.service=service;}}
By default, containers resolve singletons when usinguseClass anduseFactory.Default life time for all items in a container can be set by passing an option object to it's contructor withdefailtLifeTime attribute. Possible values:LifeTime.PerRequest (resolves instances) andLifeTime.Persistent (resolves singletons);
import{LifeTime}from'container-ioc';constcontainer=newContainer({defaultLifeTime:LifeTime.PerRequest});
You can also specify life time individually for each item in a container by specifyinglifeTime attribute.
container.register([{token:TService,useClass:Service,lifeTime:LifeTime.PerRequest}]);
container.register([{token:TService,useFactory:()=>{return{serve():void{}}},lifeTime:LifeTime.Persistent}]);
If a container can't find a value within itself, it will look it up in ascendant containers. There a 3 ways to set a parent for a container.
constparentContainer=newContainer();constchildContainer=parentContainer.createChild();
constparent=newContainer();constchild=newContainer();child.setParent(parent);
constparent=newContainer();constchild=newContainer({parent:parent});
/* Without injections */container.register([{token:'TokenForFactory',useFactory:()=>{return'any-value';}}]);/* With injections */container.register([{token:'EnvProvider',useClass:EnvProvider},{token:'TokenForFactory',useFactory:(envProvider)=>{// do somethingreturn'something';},inject:['EnvProvider']}]);
container.register([{token:'IConfig',useValue:{}}]);
container.register([App]);
Is the same as:
container.register([{token:App,useClass:App}]);
Become a contributor to this project. Feel free to submit anissue or a pull request.
seeCONTRIBUTION.md for more information.
Please see also ourCode of Conduct.
About
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.