IoC Container. Inspired by Spring Framework
What is buncha?
- IoC Container - Buncha is essential package to build a powerful, large application
- Invoke Function and Construct Object - Find the correct arguments automatically to invoke functions or construct an object from class.
- Dependency Manager - Manage dependencies of services in container. Auto detect dependency cycle.
- Annotation Scanner - Buncha uses @Service annotation to detect services in projects.
- Watch File Changes and Auto Reload -
Hate restarting application to apply the changes every time modify a file?
Buncha detects changes and reload it and all dependents for you automatically. - Fast Execution - Buncha usually spends less than
0.02 milliseconds
to parse and start invoking a function or constructing an object. See benchmark test
Install
$ npm install --save buncha
Register services manually
//Declare servicesvar { //...}var { //...}//Register services. The registration order is NOT importantvar container = new Container;container;container; //Get service by namevar reportService = container;var services = container
Invoke function and construct object
{ //...} { //...} //Buncha finds correct arguments to invoke the functionvar report1 = container; //Add missingResolver {type:"pdf"}//Missingresolver can be a function(parameterName){}var report2 = container;
Using service annotation to declare a service
Create file service/order-service.js
with annotation @Service in multi-line comment block
/** * @Service(name="orderService") * */moduleexports = OrderService; { //...}
Scan and watch
Use buncha
to scan all services in service
directory
var container = new Container;var promise = container;// .scan("service") is also OK.
We can also use .watch()
to scan and watch all changes of services to auto reload them:
var container = new (require("buncha").Container)();
var promise = container.watch(["service"]);
Default services in IoC container:
- $construct
- $invoke
- $register
- $registerByConstructor
- $resolve
- $resolveByAnnotation
Function utility
{ //...}var { this{ return name; } this{ return age; }}var user = "Tom" 10; var Fx = Fx;var parameters = Fx; //return ["name", "age"]var methods = Fx; //return ["getName", "getAge"]var annotations = Fx; //return all annotations
Benchmark
On my machine, Buncha usually spends only more 0.00122 ms
to 0.009347 ms
to parse and invoke a function
and 0.001195 ms
to 0.020473 ms
to construct an object.
Following is the result of my test:
Benchmark result of $invoke:
Wo Container: It takes 34ms to invoke 1000000 times.
W/ Container, Wo Caching: It takes 9381ms to invoke 1000000 times. Average time spent by Buncha: 0.009347ms
W/ Container, With Caching: It takes 1254ms to invoke 1000000 times. Average time spent by Buncha: 0.00122ms
Benchmark result of $construct:
Wo Container: It takes 38ms to construct 1000000 times.
W/ Container, Wo Caching: It takes 20511ms to construct 1000000 times. Average time spent by Buncha: 0.020473ms
W/ Container, With Caching: It takes 1233ms to construct 1000000 times. Average time spent by Buncha: 0.001195ms
Run the benchmark test:
$ mocha --grep "Benchmark"
Author
Tho Q Luong thoqbk.github.io
License
MIT