container-ioc
is a Dependency 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.
Features:
- Well-known Angular DI API.
- No external dependencies.
- Life Time control.
- Hierarchical containers.
- Resolves values using Classes, Factories and Values.
- Descriptive error messages.
- 97% test coverage.
Examples:
Installation:
npm install --save container-ioc
Basics:
Code examples below are written in Typescript. Check examples/javascript for examples written in Javascript.
Step 1. Define your interfaces and types.
Possible values for types: Symbol, string, Object.
; ;
Step 2. Declare dependencies with decorators Injectable and Inject.
;
Step 3. Create a container and register types in there.
; ; container.register;
Step 4. Resolve value from the container.
; app.run;
Step 2 for Javascript.
Since Javascript does not support parameter decorators, use alternative API for declaring dependencies. In this case we don't use Inject decorator. See examples/javascript for more.
@ { thisservice = service; }
Life Time control
By default, containers resolve singletons when using useClass and useFactory. Default life time for all items in a container can be set by passing an option object to it's contructor with defailtLifeTime attribute. Possible values: LifeTime.PerRequest (resolves instances) and LifeTime.Persistent (resolves singletons);
; ;
You can also specify life time individually for each item in a container by specifying lifeTime attribute.
container.register;
container.register;
Hierarchical containers
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.
1. Container.createChild() method.
;;
2. Container.setParent() method.
;; child.setParentparent;
3. Via Container's constructor with options.
;;
Using Factories
/* Without injections */container.register; /* With injections */container.register;
Using Values
container.register;
Shortcut for Classes
container.register;
Is the same as:
container.register;
Contribution
Become a contributor to this project. Feel free to submit an issue or a pull request.
see CONTRIBUTION.md for more information.
Please see also our Code of Conduct.