@@ -26,11 +26,13 @@ It can be used on browser, on react native or on node.js server code.
2626 - [ @OnlyInstantiableByContainer ] ( #the-onlyinstantiablebycontainer-annotation )
2727 - [ The Container Class] ( #the-container-class )
2828 - [ @InjectValue decorator and Constants] ( #injectvalue-decorator-and-constants )
29+ - [ Namespaces (Environments)] ( #namespaces-environments )
2930 - [ Creating temporary configurations] ( #creating-temporary-configurations )
3031 - [ Importing configurations from external file] ( #importing-configurations-from-external-file )
3132 - [ A note about classes and interfaces] ( #a-note-about-classes-and-interfaces )
3233 - [ Examples] ( #examples )
3334 - [ Using Container for testing] ( #using-container-for-testing )
35+ - [ Using Namespaces] ( #using-namespaces )
3436 - [ Browser usage] ( #browser-usage )
3537 - [ Restrictions] ( #restrictions )
3638 - [ Migrating from previous version] ( #migrating-from-previous-version )
@@ -395,6 +397,40 @@ It is possible to bind an internal property of a constant, like:
395397Container .bindName (' config.dependencyURL' ).to (' http://anewURL.com' );
396398```
397399
400+ ### Namespaces (Environments)
401+
402+ It is possible to create specific namespaces with custom configurations and then tell container to use these namespaces.
403+
404+ For example:
405+
406+ ``` typescript
407+ Container .bindName (' config.dependencyURL' ).to (' http://myURL.com' );
408+ const namespace = Container .namespace (' test' );
409+ Container .bindName (' config.dependencyURL' ).to (' http://anewURL.com' );
410+ ```
411+
412+ Only if the namespace ``` 'test' ``` is active, the ``` 'config.dependencyURL' ``` will resolve to ``` 'http://anewURL.com' ``` .
413+
414+ To use the default namespace, just call ``` Container.namespace(null) ``` .
415+
416+ If you want to remove a namespace, just call ```namespace.remove()````
417+
418+ ``` typescript
419+ const namespace = Container .namespace (' test' );
420+ namespace .remove ();
421+ ```
422+
423+ It is not possible to remove the default namespace.
424+
425+ An alias called ``` 'environment' ``` is defined for the namespace method:
426+
427+ ``` typescript
428+ Container .namespace (' test' );
429+ Container .environment (' test' ); // both commands are equivalents
430+ ```
431+
432+ Take a look at [ here] ( #using-namespaces ) for more examples of namespaces usage.
433+
398434### Creating temporary configurations
399435
400436You can use snapshot for testing or where you need to temporarily override a binding.
@@ -403,11 +439,8 @@ describe('Test Service with Mocks', () => {
403439
404440 const snapshot: Snapshot ;
405441 before (function () {
406- // Hack for lazy loading (mentioned elsewhere in docs)
407- MyIoCConfigurations .configure ();
408-
409- // Store the IoC configuration for IService
410- snapshot = Container .snapshot (IService );
442+ // Store the IoC configuration
443+ snapshot = Container .snapshot ();
411444
412445 // Change the IoC configuration to a mock service.
413446 Container .bind (IService ).to (MockService );
@@ -436,7 +469,7 @@ import { Scope } from 'typescript-ioc';
436469import * as yaml from ' js-yaml' ;
437470import * as fs from ' fs' ;
438471
439- const config = yaml .safeLoad (fs .readFileSync (configFileName , ' utf8' ));
472+ const config = yaml .safeLoad (fs .readFileSync (' service-config.yml ' , ' utf8' ));
440473
441474export default [
442475 { bind: MyType , to: MyTypeImpl },
@@ -462,6 +495,31 @@ Container.configure(config);
462495
463496You need to load the configurations only once, but before you try to use the objects that depends on these files.
464497
498+ You can create configurations for specific namespaces, like:
499+
500+ ``` typescript
501+ import { MyRepository , MyTestRepository } from ' ./my-types' ;
502+ import * as yaml from ' js-yaml' ;
503+ import * as fs from ' fs' ;
504+
505+ const config = yaml .safeLoad (fs .readFileSync (' service.config.yml' , ' utf8' ));
506+ const configTest = yaml .safeLoad (fs .readFileSync (' service.config-test.yml' , ' utf8' ));
507+ const configProd = yaml .safeLoad (fs .readFileSync (' service.config-prod.yml' , ' utf8' ));
508+
509+ export default [
510+ { bindName: ' config' , to: config },
511+ { namespace: {
512+ test: [
513+ { bindName: ' config' , to: configTest },
514+ { bind: MyRepository , to: MyTestRepository },
515+ ],
516+ production: [
517+ { bindName: ' config' , to: configProd }
518+ ]
519+ }
520+ }
521+ ];
522+ ```
465523
466524## A note about classes and interfaces
467525
@@ -586,7 +644,7 @@ describe('My Test', () => {
586644 let myService: MyService ;
587645 let snaphot: Snaphot ;
588646 beforeAll (() => {
589- snapshot = Container .snapshot (MyService );
647+ snapshot = Container .snapshot ();
590648 Container .config (mocksConfig );
591649 myService = Container .get (MyService );
592650 });
@@ -599,6 +657,44 @@ describe('My Test', () => {
599657});
600658```
601659
660+ ### Using Namespaces
661+
662+ Define configurations on a file, like ``` ioc.config.ts ``` :
663+
664+ ``` typescript
665+ import { MyRepository , MyTestRepository } from ' ./my-types' ;
666+ import * as yaml from ' js-yaml' ;
667+ import * as fs from ' fs' ;
668+
669+ const config = yaml .safeLoad (fs .readFileSync (' service.config.yml' , ' utf8' ));
670+ const configTest = yaml .safeLoad (fs .readFileSync (' service.config-test.yml' , ' utf8' ));
671+ const configProd = yaml .safeLoad (fs .readFileSync (' service.config-prod.yml' , ' utf8' ));
672+
673+ export default [
674+ { bindName: ' config' , to: config },
675+ { env: {
676+ test: [
677+ { bindName: ' config' , to: configTest },
678+ { bind: MyRepository , to: MyTestRepository },
679+ ],
680+ production: [
681+ { bindName: ' config' , to: configProd }
682+ ]
683+ }
684+ }
685+ ];
686+ ```
687+ And then import the configurations using:
688+
689+ ``` typescript
690+ import { Container } from " typescript-ioc" ;
691+ import config from ' ./ioc.config' ;
692+
693+ Container .configure (config );
694+ // Then activate the environment calling the container
695+ Container .environment (process .env .NODE_ENV );
696+ ```
697+
602698## Browser usage
603699
604700It was tested with browserify and webpack, but it should work with any other similar tool.
@@ -680,7 +776,7 @@ We had a minor change in the Snapshot handling. We don't have anymore the public
680776The new way:
681777
682778``` typescript
683- const snapshot = Container .snapshot (MyType );
779+ const snapshot = Container .snapshot ();
684780snapshot .restore ();
685781```
686782
0 commit comments