Dependency Injection Container / plus.container
https://en.wikipedia.org/wiki/Dependency_injection
That is simple dependency injection container, it allows to keep your application simple and DRY. Allows to manage services and inject dependencies. Article about this
Simple usage
var Container = ;var container = ;
ES6
"use strict"; { thisa = a; thisb = b; } container; container; container; var instance = container; // instance.should.be.instanceof(MyService); // "AA".should.equal(instance.a); // "BB".should.equal(instance.b);
Behaviour with ES6 is same as ES5 "classes" you can mix ES5 and ES6 services too.
ES6 provide/injection and custom mapping
Examples with dependency injection for ES6/ES2015
Dependency injection ES6.
/// example 1 // container;container; container; console;console; // container.c it is alias for container.get('c')// c { result: 3 }// c { result: 3 }
When we ask c
it means that container will inject service a
and b
to the c
and we will see result.
It means c
service will get to arguments c(a, b)
looks cool :)
Dependency injection ES6 with mapping.
// exmaple 2 with mappingcontainer;container;container; let remap = a: 'customA' container; console;console; // container.c it is alias for container.get('c')// c { result: 9 }// c { result: 9 }
It means c
service will get to arguments c(customA, b)
it means that we can provide custom implementation to c
service.
Register service and use ES5 examples
// define your classvar {} // register in containercontainer; var service = container; // get service// service.should.be.instanceof(MyClass); // it equals: var service = new MyClass();// AND: container.get('myService') === container.get('myService') // true
Inject dependencies
// define your classesvar {}var { // myService1.should.be.instanceof(MyClass1);} // register in containercontainer;container; var service2 = container;// service2.should.be.instanceof(MyClass2);// it equals: var service2 = new MyClass2(new MyClass1()); // AND: container.get('myService2') === container.get('myService2'); // true
Nested access
var config = host: 'yyy.com' db: host: 'localhost' container; container; // yyy.com container; // localhost
for example this way:
var config = db: host: 'localhost' port: 27017 database: 'test1' container; container
Tags, you can find/filter services by tags
Mark your services with tags and find them. It allows to build plugins.
var { return CLASS: 1 } Class1$tags = 'tag1'; var { return CLASS: 2 } Class2$tags = 'tag1' 'tag2'; container; container; var tag1services = container; // returns array of services // tag1services.length == 2, we will see both of them. var tag2services = container; // tag2services.length == 1, we will see Class2 instance var tag1_minus_tag2_services = container; // tag1_minus_tag2_services.length == 1, we will see Class1 instance
Loading services with environment
Dir
.
|-- container.js
|-- container_dev.js
|-- container_prod.js
`-- container_test.js
// container.js example module { var { } var { } container; container; container; container; }
Usage
Your application's code.
// app.js // loading container from the folder, it allows to manage environmentsvar container = Container; container // it equals new Class1()container // it equals new Class2()container // it equals new Service3(new Class1())container // it equals new Service4(new Class1(), new Class2()) // BUT: container.get('service4') === container.get('service4') // true // :)
Run this!
node app.js
This code load container.js code in this order:
container.js
// register common servicescontainer_dev.js
// register environment depended services and you can override common services
If we use environment it looks like:
NODE_ENV=test node app.js
in this case it loads container in this order:
container.js
// register common servicescontainer_test.js
// register environment (test) dependent services and you can override common services
Have a fun and manage your services! +1G Team
Misc
How to use in the browser?
var container =
Aliases
Services registration
container container /// same
Container creating
var container = ;var container = //same