What is dependencyjs?
dependencyjs is available on npm. To install it, type:
$ npm install dependencyjs
You can also use it within the browser; install via npm and use the
ComponentRegistry.js file found within the download. For example:
Limitations: loading configurations dynamically from other files is not suppoerted on the browser. It must be bundled together (using browserify or webpack).
You can register base classes with their implementation. Lets take an example of nodejs application
below is the base class
now create a concrete implementation of BaseSample class
Now register class with its concrete implementation
;;;//register class object;registry.registerBaseSample, sample;
Resolve class object to use it anywhere in code
Dependencies can be registered with extra parameter so that we can have multiple implementation of base class registered in registry.
lets extend same Base sample in another class and register both of them in registry
Now register both of them in registry
;;;;//register class object;;registry.registerBaseSample, sample, "sample";registry.registerBaseSample, secondSample, "secondSample";
Resolve class object with resolver in your code
Loading dependencies from configuration
dependencyjs provide a functionality where you can load classes dynamically from json configurations.
How to configure json
Details of each field
1. Container: Container can have multiple class registration. 2. Register: In register, you can mention detail about base class and child class to register the dependency. a. base: typeName : This will contain name of the exported base class. In our example about, it could be "BaseSample" sourceType: You can use a base class from a node package or it could exist in your local system. So there are two possible values -> "fs" or "package" sourceInfo: If sourceType is package then this field will contain name of the package. If source type is "fs" then this field will contain localtion of your file which contains base class. For example, location of Sample class is "/BaseSample". b. map typeName : This will contain name of the exported concrete implementation class. In our example, it could be "Sample". sourceType: You can use a concrete class from a node package or it could exist in your local system. So there are two possible values -> "fs" or "package" sourceInfo: If sourceType is package then this field will contain name of the package. If source type is "fs" then this field will contain localtion of your file which contains concrete class. For example, location of Sample class is "/Sample". c. resolver : If a base class have multiple implementation and you want to get any kind of implementation at run time, you can use resolver. This will help you to identify classes uniquely.
Here is example of one json configuration
How to load json configuration
In registry, a method has been introduced -
You can use above method to load json configurations. This method needs Config object as parameter. You can find it from dependencyjs. Below are the detail of Config object
- Config: This class basically contains two properties. - appDirectory: You need to initialize this property with main execution directory. - configFolder: This is the folder path where you keep all json configurations to register them in registry.
Below is the example of reading json configuration.
;;config.appDirectory = "Main application directory"config.configFolder = "where you contains all json"registry.loadConfigurationconfig;
All your classes will be register which has been mentioned in json files. If not, you will get appropriate error message.
Now you can use registry as the way explained in usage section
Feel free to reach out to any of the core contributors with your questions or concerns. We will do our best to respond in a timely manner.